aio-libs / async-timeout

asyncio-compatible timeout class

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

code is never stopped

NeedNot opened this issue · comments

in the end i want to run math but end it if it takes longer than x to complete but even this doesn't even stop

           async with timeout(5):
                await run_formula()

and

async def run_formula():
    for i in range(100):
        print(i)
        time.sleep(1)

it should stop at 0, 1, 2, 3, 4 but instead it counts all the way to 100

That's not how asyncio works, we can't cancel if you don't yield back to the event loop anywhere (e.g. with an await).

Use await asyncio.sleep(1), not time.sleep(), which is a blocking function resulting in your application not being able to handle any other async tasks.

Ah how do I abort an execution from an external library without support to async calls? I'm using the package
rdkit and the function rdkit.Chem.rdDetermineBonds.DetermineBondOrders sometimes takes forever to execute, and I want to add a timeout funcionality. I was hoping async-timeout would save me, but when I write this code:

async with timeout(1):
    await rdDetermineBonds.DetermineBondOrders(conn_mol, charge=mol_charge)

To force rdDetermineBonds.DetermineBondOrders abort after 1 second, raising an error (that I will catch as Exception in my code). But it didn't work! Am I having a wrong understanding about how async works? My problem can be solved using this library?

EDIT: I'm not willing to deal with rdkit source code, so I wish an external solution.

It should work, so I'd file a bug against rdkit if it doesn't. The reasons that the timeout doesn't work are likely that rdkit isn't yielding to the event loop (which they should be if it's meant to be an async interface), or they are catching and suppressing the CancelledError.