Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPython: Running commands asynchronously in the background

Say I have a Python command or script that I want to run from IPython asynchronously, in the background, during an IPython session.

I would like to invoke this command from my IPython session and be notified when it is done, or if something fails. I don't want this command to block my IPython prompt.

Are there any IPython magics that support this? If not, what is the recommended way of running asynchronous jobs/scripts/commands (that run locally) on IPython?

For example, say I have a function:

def do_something():
   # This will take a long time
   # ....
   return "Done"

that I have in the current namespace. How I can I run it to the background and be notified when it is done?

like image 905
Amelio Vazquez-Reina Avatar asked Mar 18 '14 13:03

Amelio Vazquez-Reina


People also ask

How can I run an external command asynchronously from Python?

To run an external command asynchronously from Python, we can use the asyncio. create_subprocess_exec method. to run a process asynchronously with asyncio.

Which of the following is used to run system commands from IPython?

IPython bridges this gap, and gives you a syntax for executing shell commands directly from within the IPython terminal. The magic happens with the exclamation point: anything appearing after ! on a line will be executed not by the Python kernel, but by the system command-line.

What is IPython magic?

The magic commands, or magics, are handy commands built into the IPython kernel that make it easy to perform particular tasks, for example, interacting Python's capabilities with the operating system, another programming language, or a kernel. IPython provides two categories of magics: line magics and cell magics.


2 Answers

Yes, try (in a cell):

%%script bash --bg --out script_out

sleep 10
echo hi!

The script magic is documented along with the other IPython magics. The necessary argument here is -bg to run the below script in the background (asynchronously) instead of the foreground (synchronously).

GitHub Issue #844 is now resolved.

like image 164
Aron Ahmadia Avatar answered Oct 31 '22 01:10

Aron Ahmadia


There used to be a magic function in iPython that would let you do just that: https://github.com/ipython/ipython/wiki/Cookbook:-Running-a-file-in-the-background

However, it seems that it was removed and is still pending to come back in newer versions: https://github.com/ipython/ipython/issues/844

It still provides a library to help you achieve it, though: http://ipython.org/ipython-doc/rel-0.10.2/html/api/generated/IPython.background_jobs.html

like image 42
jminuscula Avatar answered Oct 31 '22 02:10

jminuscula