Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait for task created by create_task() to complete?

Tags:

I wrote a test program to try out using create_task() that needs to wait until the created task completes.

I tried using loop.run_until_complete() to wait for task completion, but it results in an error with a traceback.

/Users/jason/.virtualenvs/xxx/bin/python3.5 /Users/jason/asyncio/examples/hello_coroutine.py
    Traceback (most recent call last):
    Test
      File "/Users/jason/asyncio/examples/hello_coroutine.py", line 42, in <module>
    Hello World, is a task
        loop.run_until_complete(test.greet_every_two_seconds())
      File "/Users/jason/asyncio/asyncio/base_events.py", line 373, in run_until_complete
        return future.result()
      File "/Users/jason/asyncio/asyncio/futures.py", line 274, in result
        raise self._exception
      File "/Users/jason/asyncio/asyncio/tasks.py", line 240, in _step
        result = coro.send(None)
      File "/Users/jason/asyncio/examples/hello_coroutine.py", line 33, in greet_every_two_seconds
        self.a()
      File "/Users/jason/asyncio/examples/hello_coroutine.py", line 26, in a
        t = loop.run_until_complete(self.greet_every_one_seconds(self.db_presell))
      File "/Users/jason/asyncio/asyncio/base_events.py", line 361, in run_until_complete
        self.run_forever()
      File "/Users/jason/asyncio/asyncio/base_events.py", line 326, in run_forever
        raise RuntimeError('Event loop is running.')
    RuntimeError: Event loop is running.

The test code is as follows. The function a() must not be a coroutine,

How can I wait for the task until complete?

import asyncio

class Test(object):

    def __init__(self):
        print(self.__class__.__name__)
        pass
    @asyncio.coroutine
    def greet_every_one_seconds(self, value):
            print('Hello World, one second.')
            fut = asyncio.sleep(1,result=value)
            a = yield from fut
            print(a)

    def a(self):

        loop = asyncio.get_event_loop()

        task=loop.run_until_complete(self.greet_every_one_seconds(4))
        #How can i wait for the task until complete?

    @asyncio.coroutine
    def greet_every_two_seconds(self):
        while True:
            self.a()
            print('Hello World, two seconds.')
            yield from asyncio.sleep(2)


if __name__ == '__main__':
    test = Test()
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(test.greet_every_two_seconds())
    finally:
        loop.close()
like image 384
user746620 Avatar asked Mar 21 '16 07:03

user746620


People also ask

What does await do in Asyncio?

The keyword await passes function control back to the event loop. (It suspends the execution of the surrounding coroutine.) If Python encounters an await f() expression in the scope of g() , this is how await tells the event loop, “Suspend execution of g() until whatever I'm waiting on—the result of f() —is returned.

How do you stop a loop in Python event?

Run the event loop until stop() is called. If stop() is called before run_forever() is called, the loop will poll the I/O selector once with a timeout of zero, run all callbacks scheduled in response to I/O events (and those that were already scheduled), and then exit.

How does async await work in Python?

An async function uses the await keyword to denote a coroutine. When using the await keyword, coroutines release the flow of control back to the event loop. To run a coroutine, we need to schedule it on the event loop. After scheduling, coroutines are wrapped in Tasks as a Future object.

How do you create a task in Python?

Creating TasksWrap the coro coroutine into a Task and schedule its execution. Return the Task object. If name is not None , it is set as the name of the task using Task. set_name() .


1 Answers

How can i wait for the task until complete?

loop.run_until_complete(task) in an ordinary function. Or await task in a coroutine.

Here's a complete code example that demonstrates both cases:

#!/usr/bin/env python3
import asyncio

async def some_coroutine(loop):
    task = loop.create_task(asyncio.sleep(1))  # just some task
    await task # wait for it (inside a coroutine)

loop = asyncio.get_event_loop()
task = loop.create_task(asyncio.sleep(1)) # just some task
loop.run_until_complete(task) # wait for it (outside of a coroutine)
loop.run_until_complete(some_coroutine(loop))
like image 182
jfs Avatar answered Oct 20 '22 05:10

jfs