How to run async function forever (Python)

10,469

Solution 1

run_forever doesn't mean that an async function will magically run forever, it means that the loop will run forever, or at least until someone calls loop.stop(). To literally run an async function forever, you need to create an async function that does that. For example:

async def some_function():
    async with something as some_variable:
        # do something

async def forever():
    while True:
        await some_function()

loop = asyncio.get_event_loop()
loop.run_until_complete(forever())

This is why run_forever() doesn't accept an argument, it doesn't care about any particular coroutine. The typical pattern is to add some coroutines using loop.create_task or equivalent before invoking run_forever(). But even an event loop that runs no tasks whatsoever and sits idly can be useful since another thread can call asyncio.run_coroutine_threadsafe and give it work.

Solution 2

I'm unsure as to exactly what you mean when you say I'm not sure how to start the function. If you're asking the question in the literal sense:

loop = asyncio.get_event_loop()
loop.run_forever()

If you wish to add a function to the loop before initialising the loop then the following line prior to loop.run_forever() will suffice:

asyncio.async(function())

To add a function to a loop that is already running you'll need ensure_future:

asyncio.ensure_future(function(), loop=loop)

In both cases the function you intend to call must be designated in some way as asynchronous, i.e. using the async function prefix or the @asyncio.coroutine decorator.

Share:
10,469

Related videos on Youtube

Tearzz
Author by

Tearzz

-

Updated on June 04, 2022

Comments

  • Tearzz
    Tearzz almost 2 years

    How do I use asyncio and run the function forever. I know that there's run_until_complete(function_name) but how do I use run_forever how do I call the async function?

    async def someFunction():
        async with something as some_variable:
            # do something
    

    I'm not sure how to start the function.

    • scharette
      scharette almost 6 years
      You know there's run_until_complete(function_name), could you provide the documentation to it ?
    • Tearzz
      Tearzz almost 6 years
      Sorry for the late response, an example can be found here in the docs: docs.python.org/3/library/…
  • Tearzz
    Tearzz almost 6 years
    But this doesn't start the function, it never calls the function.
  • amitchone
    amitchone almost 6 years
    You need to add the task containing the function to the loop.
  • Tearzz
    Tearzz almost 6 years
    I don't understand. How do I call the function "async def someFunction()" and make it loop forever?
  • amitchone
    amitchone almost 6 years
    I've added extra detail to my answer
  • Tearzz
    Tearzz almost 6 years
    I literally still don't understand. If I use asyncio.async(function()) at the end of my script the script just ends, as if it's not looping. If I add loop.run_forever() after it still doesn't work. I really don't understand, how do I start a function that runs forever...
  • user4815162342
    user4815162342 almost 6 years
    Note that asyncio.async is deprecated and is an alias for asyncio.ensure_future. Also, when the argument is a coroutine, loop.create_task() is preferred.