Do I need to include all my function in eventloop?

If I have an event loop and an asynchronous function:

# asyncio_coroutine_forever.py


import asyncio

async def hello_world():
    print('Hello World')
    await good_evening()


async def good_evening():
    print('Good Evening')


loop = asyncio.get_event_loop()

try:
    loop.run_until_complete(hello_world())
    loop.run_forever()
finally:
    print('closing event loop')
    loop.close()

The hellp_world function is called by eventloop. The second good_evening function is in the same eventloop or not?

Is it necessary to include the second function in the loop or only the first? If I use this example, the second use cycle and context switching?

+4
source share
1 answer
  • Yes, it run_until_completewill fulfill your future hello_worlduntil it returns or works (it will also block your flow in the process).

  • You do not need to enable the second function unless you plan it yourself.

run_until_complete , , , , , run_util_complete.

+4

Source: https://habr.com/ru/post/1679379/


All Articles