Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code is never stopped #268

Closed
NeedNot opened this issue Nov 27, 2021 · 3 comments
Closed

code is never stopped #268

NeedNot opened this issue Nov 27, 2021 · 3 comments

Comments

@NeedNot
Copy link

NeedNot commented Nov 27, 2021

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

@Dreamsorcerer
Copy link
Member

Dreamsorcerer commented Nov 27, 2021

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.

@viniavila
Copy link

viniavila commented Mar 13, 2023

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.

@Dreamsorcerer
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants