1

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.

2
  • You know there's run_until_complete(function_name), could you provide the documentation to it ? Commented May 22, 2018 at 18:09
  • Sorry for the late response, an example can be found here in the docs: docs.python.org/3/library/… Commented May 31, 2018 at 11:14

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

6 Comments

But this doesn't start the function, it never calls the function.
You need to add the task containing the function to the loop.
I don't understand. How do I call the function "async def someFunction()" and make it loop forever?
I've added extra detail to my answer
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...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.