You can't have it both ways. If it's a coroutine, you can't get its result
in the callback. But that shouldn't be a problem -- you can just put any
processing of the result you need in the coroutine. In effect, the only
purpose of the callback then is to start the coroutine running. If you have
an existing coroutine whose result you want to process, just wrap it in
another coroutine which calls it (using yield from) and then does the
processing.

On Wed, Aug 5, 2015 at 7:21 PM, Wellington Cordeiro <willy123...@gmail.com>
wrote:

> How do you unwrap the result of the coroutine wrapped in the task though?
>
> On Wednesday, August 5, 2015 at 4:02:24 AM UTC-6, Guido van Rossum wrote:
>>
>> To start a coroutine from a callback, you wrap the coroutine in a Task.
>> That's all. E.g. (untested)
>>
>> from asyncio import coroutine, get_event_loop
>>
>> @coroutine
>> def coro():
>>     ...
>>     yield from something()
>>     ...
>>
>> # callback
>> def heartbeat():
>>     get_event_loop().create_task(coro())
>>     get_event_loop().call_later(n, heartbeat)
>>
>> heartbeat()  # Get it all started
>>
>> On Wed, Aug 5, 2015 at 5:46 AM, Wellington Cordeiro <willy...@gmail.com>
>> wrote:
>>
>>> I'm trying to add a system where a "heartbeat" message is sent to a
>>> server every n seconds by my client, but I'm having trouble with the
>>> loop.call_later or call_at methods. They use callbacks but I'm trying to
>>> run a coroutine at the set interval without blocking. The conditions I'm
>>> trying to work within are
>>>
>>> 1. countdown from n, when we reach zero send the heartbeat
>>> 2. If a message is received from the server before we hit zero, reset
>>> the timer
>>> 3. If we send a different message before we hit zero, reset the timer
>>>
>>> I'm just not sure how to implement something like this with asyncio, I
>>> tried using the threading.Timer class but since that creates another thread
>>> my client called the loop.stop() method before the interval and closed its
>>> connection early.
>>>
>>
>>
>>
>> --
>> --Guido van Rossum (python.org/~guido)
>>
>


-- 
--Guido van Rossum (python.org/~guido)

Reply via email to