Search code examples
pythonpython-asynciogevent

How to poll python asyncio task status


With gevent, I can monitor a long running function like so:

    greenlet = gevent.Greenlet(long_running_task, *args, **kwargs)
    greenlet.start()
    while not greenlet.ready():
        send_heartbeat()
        gevent.sleep(heartbeat_interval)
    if greenlet.successful():
        send_success(greenlet.value)
    else:
        send_failure(exception=greenlet.exception)

How would I do this with asyncio? I've tried the following but I'm stuck:

    loop = asyncio.get_event_loop()
    async def send_heartbeat(heartbeat_interval=15):
        send_heartbeat()
        asyncio.sleep(heartbeat_interval)

    await asyncio.sleep(1)
    loop.run_until_complete(asyncio.wait([long_running_task(*args, **kwargs), send_heartbeat()]))
    loop.close()

How would I change this to get the intended behavior?


Solution

  • You can schedule the long running task with ensure_future (but not awaiting it). In return you'll get a Future object that have done method that is similar to ready

    async def some_job():
        future = asyncio.ensure_future(long_running_task(*args, **kwargs))
        while not future.done():
            await send_heartbeat(heartbeat_interval=15)
    
        try:
            result = future.result()
        except asyncio.CancelledError:
            # the task has been cancelled
        except Exception:
            # some exception was raised in long running task
    
    loop = asyncio.get_event_loop()
    loop.run_until_complete(some_job())
    loop.close()