Search code examples
pythonpython-3.xpython-asyncio

How do I get my async tasks to run in the background and give me a REPL in the foreground?


I'm trying to wrap my head around asyncio (Python 3.7).

import asyncio
loop = asyncio.get_event_loop()
loop.set_debug(True)
async def tick():
    for i in range(3):
        print(i)
        asyncio.sleep(1)
    print("Finishing")

task = loop.create_task(tick())

Currently, this seems to produce no output. What I'm trying to do is to get it run my async code, while simultaneously returning me to a REPL while it runs.

My hope is to be able to receive data from websockets, and still be able to type commands inside the REPL to explore what's received.

What am I doing wrong?


Solution

  • It turns out it's not possible to run an async task while having a REPL, unless you start python as follows:

    python -m asyncio