import trio
work_available = trio.Event()
async def get_work():
while True:
work = check_for_work()
if not work:
await work_available.wait()
else:
return work
def add_work_to_pile(...):
...
if work_available.statistics().tasks_waiting:
global work_available
work_available.set()
work_available = trio.Event()
In this Python-like code example I get work in bursts via add_work_to_pile()
. The workers which get work via get_work()
are slow. So most of the time add_work_to_pile()
is called there will be no one waiting on work_available
.
Which is better/cleaner/simpler/more pythonic/more trionic/more intended by the trio developers?
- checking if someone is looking for the
Event()
viastatistics().tasks_waiting
, like in the example code, ...or... - unconditionally
set()
setting theEvent()
and creating a new one each time? (Most of them in vain.)
Furthermore... the API does not really seem to expect regular code to check if someone is waiting via this statistics()
call...
I don’t mind spending a couple more lines to make things clearer. But that goes both ways: a couple CPU cycles more are fine for simpler code...