Recipe request: asyncio "spin off coroutine"

Chris Angelico rosuav at gmail.com
Wed Dec 14 16:05:58 EST 2016


On Thu, Dec 15, 2016 at 7:53 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Wed, Dec 14, 2016 at 12:53 PM, Chris Angelico <rosuav at gmail.com> wrote:
>> On Thu, Dec 15, 2016 at 6:27 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
>>> Chris Angelico <rosuav at gmail.com>:
>>>
>>>>     asyncio.spin_off(parallel()) # ???
>>>>
>>>> [...]
>>>>
>>>> What code should go on the "???" line to accomplish this?
>>>
>>> asyncio.ensure_future(parallel())
>>
>> Hmm. I tried that but it didn't work in the full program (it hung the
>> calling coroutine until completion). But it does work in the toy
>> example I posted here. Weird. Must be something else that's wrong,
>> then. I'll keep poking around, thanks.
>
> Did you just use "asyncio.ensure_future(parallel())" in the full
> program or did you throw in an await by mistake?

I didn't await that, no. Have just pinned down the problem, and it's a
total facepalm moment: the secondary task had gotten stuck in an
infinite loop with no await points in it. So technically they _would_
both have been scheduled when I did it as per Marko's suggestion, but
I couldn't tell. Whoooooooops. Sorry about that!

Still might be worth having a simple recipe in the docs though; it
would have saved me the trouble of digging through my "fork" code.

ChrisA


More information about the Python-list mailing list