I have an iterable of coroutines which run in parallel.
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop=loop)
_lock = asyncio.Lock()
asyncio.run(apply_coroutines())
async def apply_coroutines():
await asyncio.gather(
coroutine1(),
coroutine2(),
coroutine3(),
coroutine4(),
)
async def another_coroutine():
print('another method')
I have 2 conditions. the first condition triggers, say, coroutine2()
. Once triggered, I have a call_later
method for a future event. I need to suspend all coroutines once coroutine2()
is triggered, until a condition cond2
is satisfied.
What I tried is below where I delayed 10 second the call:
async def coroutine2():
if cond1:
# ...
_lock.acquire()
loop.call_later(10, another_coroutine)
_lock.release()
how can I implement a condition
cond2
in place of delaying 10 seconds?Am I right with the way I am using the Lock? Because with
asyncio.gather()
I have interference of coroutines. Ex: one of them starts output to IO, in a text file, but the second takes turn and finishes IO output.