Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 313
Patrick Avatar asked Nov 16 '25 06:11

Patrick


1 Answers

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()
like image 105
kwarunek Avatar answered Nov 17 '25 22:11

kwarunek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!