Can an asyncio event loop run in the background without suspending the Python interpreter?

前端 未结 2 403
孤独总比滥情好
孤独总比滥情好 2020-12-01 03:50

The documentation for asyncio gives two examples for how to print \"Hello World\" every two seconds: https://docs.python.org/3/library/asyncio-eventloop.html#asyncio-hello-w

相关标签:
2条回答
  • 2020-12-01 04:09

    Edit:

    If using Python 3.8 or above, you should use the asyncio repl, as explained in zeronone's answer. If using 3.7 or lower, you can use this answer.


    You can run the event loop inside a background thread:

    >>> import asyncio
    >>> 
    >>> @asyncio.coroutine
    ... def greet_every_two_seconds():
    ...     while True:
    ...         print('Hello World')
    ...         yield from asyncio.sleep(2)
    ... 
    >>> def loop_in_thread(loop):
    ...     asyncio.set_event_loop(loop)
    ...     loop.run_until_complete(greet_every_two_seconds())
    ... 
    >>> 
    >>> loop = asyncio.get_event_loop()
    >>> import threading
    >>> t = threading.Thread(target=loop_in_thread, args=(loop,))
    >>> t.start()
    Hello World
    >>> 
    >>> Hello World
    

    Note that you must call asyncio.set_event_loop on the loop, otherwise you'll get an error saying that the current thread doesn't have an event loop.

    If you want to interact with the event loop from the main thread, you'll need to stick to loop.call_soon_threadsafe calls.

    While this kind of thing is an ok way to experiment in the interpreter, in actual programs, you'll probably want all your code running inside the event loop, rather than introducing threads.

    0 讨论(0)
  • 2020-12-01 04:23

    With Python 3.8, you can use the new asyncio REPL.

    $ python -m asyncio
    >>> async def greet_every_two_seconds():
    ...     while True:
    ...         print('Hello World')
    ...         await asyncio.sleep(2)
    ...
    >>> # run in main thread (Ctrl+C to cancel)
    >>> await greet_every_two_seconds()
    ...
    >>> # run in background
    >>> asyncio.create_task(greet_every_two_seconds())
    
    0 讨论(0)
提交回复
热议问题