Asyncio -- delayed calculation

Chris Angelico rosuav at gmail.com
Mon Nov 28 20:02:49 EST 2016


On Tue, Nov 29, 2016 at 11:20 AM, Steve D'Aprano
<steve+python at pearwood.info> wrote:
> On Tue, 29 Nov 2016 02:53 am, Ian Kelly wrote:
>
>> In order for the coroutines to actually do anything, you need to
>> schedule them in some way with the event loop. That could take the
>> form of awaiting them from some other coroutine, or passing them
>> directly to loop.run_until_complete or event_loop.create_task, or as
>> Chris suggested awaiting them as an aggregate.
>
> I thought that's what I had done, by calling
>
> loop.run_until_complete(main())

That's invoking a single task, main(). If you were to write this code
using threads, main would need to be thread-aware so that it can fork
appropriately - it needs to start new threads for the subtasks. Tasks
in asyncio are like cooperatively-switched threads or threadlets, and
if you want them to run in parallel, you have to instruct them to run
as separate tasks.

ChrisA



More information about the Python-list mailing list