[IPython-dev] Asyncio extensions for IPython

Martin Teichmann lkb.teichmann at gmail.com
Tue Sep 30 10:53:06 EDT 2014


Hi,

I've been working with the Python 3.4 asyncio package, which
uses the yield from statement to perform asynchronous I/O.

I realized that such an approach might also be useful for IPython,
as it allows to start tasks in the background without stopping the
currently running event loop, and without using threads. So I wrote
an asyncio extension for IPython. It allows to use the yield from
statement at the command line.

The important difference to normal commands is, that while they are
executed the currently running event loop continues, which is most
visible if you are running something like matplotlib. So, while the
I/O stuff is running "in the background", the matplotlib Qt window is
still responsive.

To give an example (don't forget you need python 3.4)

    In [1]: %load_ext yf  # this is my extension
    In [2]: from asyncio import sleep, async
    In [3]: def f():
       ...:    yield from sleep(3)
       ...:    print("done")
       ...:    return "returned"
    In [4]: yield from f()
     #[wait three seconds]
    done
    Out[4]: 'returned'
    In [5]: async(f())
    Out[5]: Task(<f>)<PENDING>
    In [6]: #[wait three seconds, or type other commands] done

this is a trivial example which just sleeps, but the point is, it could do
anything else, especially I/O, and still the GUI would be responsive.

I put this extension onto github as

https://github.com/tecki/ipython-yf

Greetings

Martin



More information about the IPython-dev mailing list