Asyncio run_in_executor() coroutine

2,435 views
Skip to first unread message

Ons

unread,
Aug 31, 2015, 9:05:33 AM8/31/15
to python-tulip
Hello,

I am using asyncio event loop in my project and i need to make non blocking calls of lets say a coroutine called async.

    @asyncio.coroutine
    def async(attr1, attr2):
         //my coroutine code here

 loop = asyncio.get_event_loop()
 result = yield from loop.run_in_executor(None, async('attr1', 'attr2'))
 return result

I am getting the following error : TypeError: 'Task' object is not callable.

How can i run a coroutine in a non blocking mode with asyncio ?

Thanks.

Victor Stinner

unread,
Sep 1, 2015, 4:09:04 AM9/1/15
to Ons, python-tulip
Hi,

run_in_executor() should only be used on regular blocking function,
not on asyncio asynchronous functions (coroutines, tasks, etc.)

You should call run_in_executor() inside a coroutine:

@asyncio.coroutine
def async(attr1, attr2):
...
yield loop.run_in_executor(None, time.sleep, 5)
...
return 1+1

Victor

Ons

unread,
Sep 1, 2015, 12:16:10 PM9/1/15
to python-tulip
Hi Victor,

Thank you for the response.

In fact what i want to do is to run tasks in parallel and return the result and not a future.

From what i understood, with asyncio event loop, tasks are executed in an asynchronous manner in the same thread. But if i need to execute tasks in parallel, do i have other options other than running an event loop in a separate thread for each task ?

Victor Stinner

unread,
Sep 1, 2015, 12:25:11 PM9/1/15
to Ons, python-tulip
If you want to get all results at once, you can use asyncio.gather:
https://docs.python.org/dev/library/asyncio-task.html#asyncio.gather

By the way, we should write examples for asyncio task functions. It's
not obvious how to use them.

Victor

Ons

unread,
Sep 2, 2015, 3:12:31 AM9/2/15
to python-tulip
The tasks are completely independents and i don't want to wait until asyncio gathers all the results, instead i want it to run them in parallel and return the result whenever it is ready.

The only way i have in mind that allows me to do that is to run tasks in different threads.


On Monday, August 31, 2015 at 2:05:33 PM UTC+1, Ons wrote:

Guido van Rossum

unread,
Sep 2, 2015, 10:40:19 AM9/2/15
to Ons, python-tulip
Why are you trying to use asyncio for this? What you are doing looks like it needs threads, not asyncio tasks.
--
--Guido van Rossum (python.org/~guido)
Reply all
Reply to author
Forward
0 new messages