[Python-ideas] Yielding from the command line

Petr Viktorin encukou at gmail.com
Thu Sep 11 16:04:58 CEST 2014


On Thu, Sep 11, 2014 at 3:42 PM, Martin Teichmann
<lkb.teichmann at gmail.com> wrote:
> Hi List,
>
> I'm currently trying to convince my company that asyncio is a great
> thing. After a lot of critique, the newest thing is, people complain:
> I cannot test my code on the command line! And indeed they are
> right, a simple
>
>    a = yield from some_coroutine()
>
> is not possible on the command line, and doesn't make sense.

For running asyncio code from a non-asyncio context (in tests or on
the REPL), you can use:

def run_sync(future):
    return asyncio.get_event_loop().run_until_complete(future)

a = run_sync(some_coroutine())


> Wait a minute, really?
>
> Well, it could make sense, in an asyncio-based command line.
> I am thinking about a python interpreter whose internal loop is
> something like
>
>     @coroutine
>     def commandline():
>         while True:
>             cmd = yield from input_async()
>             code = compile(cmd, "<stdin>", "generator")
>             yield from exec(code)
>
> A new compile mode would allow to directly, always create a
> generator, and exec should be certainly be able to handle this.
>
> I think this would not only make people happy that want to test
> code on the command line, but also all those people developing
> command line-GUI combinations (IPython comes to mind),
> which have to keep several event loops in sync.

That sounds like something you could experiment with in a module on pypi.


More information about the Python-ideas mailing list