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?
To run an external command asynchronously from Python, we can use the asyncio. create_subprocess_exec method. to run a process asynchronously with asyncio.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With