Trio.sleep for some time or until an event occurs

hello,
I’d like to implement the equivalent of the WaitForSingleObject Windows Api with trio but I’m not sure how to do it the proper way

I’d like to wait on an event or for a certain amount of time (like a sleep call that would be interrupted if event has changed during it’s duration)

thanks

Hi @edns, welcome on the forum! I’m not sure I understand your problem correctly, but I will suggest you a code snippet so you can tell me if this solves your problem:

with trio.move_on_after(timeout):
    await my.event.wait()

Is this what you are looking for?

I did something like:

cancel_scope = trio.CancelScope()

with cancel_scope:
	await trio.sleep(timeout)

on my event I was calling cancelscope.cancel()

your version is definitely more readable, thank you very much!