How to run async function forever (Python)
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.
Related videos on Youtube
Comments
-
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 userun_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 almost 6 yearsYou know there's
run_until_complete(function_name)
, could you provide the documentation to it ? -
Tearzz almost 6 yearsSorry for the late response, an example can be found here in the docs: docs.python.org/3/library/…
-
-
Tearzz almost 6 yearsBut this doesn't start the function, it never calls the function.
-
amitchone almost 6 yearsYou need to add the task containing the function to the loop.
-
Tearzz almost 6 yearsI don't understand. How do I call the function "async def someFunction()" and make it loop forever?
-
amitchone almost 6 yearsI've added extra detail to my answer
-
Tearzz almost 6 yearsI 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 addloop.run_forever()
after it still doesn't work. I really don't understand, how do I start a function that runs forever... -
user4815162342 almost 6 yearsNote that
asyncio.async
is deprecated and is an alias forasyncio.ensure_future
. Also, when the argument is a coroutine,loop.create_task()
is preferred.