python3 -Get result from async method

Question:

I have written a simple scraping program using asyncio. Here are my code snippets:

loop = asyncio.get_event_loop()
task = loop.create_task(conSpi.parse(arguments.url))
value = loop.run_until_complete(asyncio.wait([task]))
loop.close()

I want to print the result being returned in value. Rather than printing the variable’s value, it prints something like this:

{<Task finished coro=<ConcurrentSpider.parse() done, 
 defined at /home/afraz/PycharmProjects/the-lab/concurrentspider.py:28> result=3>}

How can I get the result only and not get rest printed?

Asked By: Afraz Ahmad

||

Answers:

The simplest approach is to write

value = loop.run_until_complete(task)

That only works if you want to wait on one task.
If you need more than one task, you’ll need to use asyncio.wait correctly.
It returns a tuple containing completed and pending futures. By default though, the pending futures will be empty because it waits for all futures to complete.

So something like

done, pending = loop.run_until_complete(asyncio.wait( tasks))
for future in done:
    value = future.result() #may raise an exception if coroutine failed
    # do something with value
Answered By: Sam Hartman

In more recent Python versions, I’d suggest asyncio.run:

import asyncio


async def do_task(duration):
    await asyncio.sleep(duration)
    return duration


if __name__ == "__main__":
    result = asyncio.run(do_task(3))
    print(result) # => 3

If you have multiple tasks to run in parallel:

import asyncio


async def do_task(duration):
    await asyncio.sleep(duration)
    return duration


async def do_multiple_tasks():
    return await asyncio.gather(do_task(3), do_task(1), do_task(2))


if __name__ == "__main__":
    result = asyncio.run(do_multiple_tasks())
    print(result) # => [3, 1, 2]
Answered By: ggorlen