Given this function:
async def read_data(self, stream: UStreams) -> None:
while True:
read_bytes = await stream.read(MAX)
#handle the bytes
This however will keep the function running forever, of course.
I’d like to have this function do this, but also shutdown (therefore exit the function) in case an event occurs:
async def read_data(self, stream: UStreams, evt: trio.Event) -> None:
while True:
read_bytes = await stream.read(MAX) || await evt.wait() # <- how do I do this?
#handle the bytes or exit
I can’t simply put evt.wait()
as first statement in the loop, as the stream would never be read (and the other way around also doesn’t work, as then the event would never be awaited if there is no stuff to read). gather
type of code also doesn’t help, I don’t want to wait for both: only to the first one occurring (in go
this would be solved with a select
statement).