by Guest » 04 Jan 2025, 06:42
Ich brauche function_B, um auf function_A zu warten. Die Funktion „wait_for_action_to_be_done()“ erledigt den Job, aber ich hätte gerne etwas Bequemeres mit „wait_for(action_to_wait)“. Das Problem besteht darin, dass in wait_for(action_to_wait) die Variable action_to_wait in der Bedingung in der while-Schleife nie aktualisiert wird. Ich denke, so etwas wie ein Zeiger würde helfen.
Code: Select all
import asyncio
action_done = False
async def function_A(delay):
global action_done
while True:
await asyncio.sleep(delay)
action_done = True
break
async def wait_for(action_to_wait):
print("wait for action to be done...")
while not action_to_wait:
await asyncio.sleep(0.1)
async def wait_for_action_to_be_done():
print("wait for action to be done...")
while not action_done:
await asyncio.sleep(0.1)
async def function_B():
await wait_for_action_to_be_done() # OK, works
# await wait_for(action_done) # KO, infinite while loop as the condition is never updated
print("performing next action...")
async def main():
await asyncio.gather(function_A(2), function_B())
asyncio.run(main())
Ich brauche function_B, um auf function_A zu warten. Die Funktion „wait_for_action_to_be_done()“ erledigt den Job, aber ich hätte gerne etwas Bequemeres mit „wait_for(action_to_wait)“. Das Problem besteht darin, dass in wait_for(action_to_wait) die Variable action_to_wait in der Bedingung in der while-Schleife nie aktualisiert wird. Ich denke, so etwas wie ein Zeiger würde helfen.
[code]import asyncio
action_done = False
async def function_A(delay):
global action_done
while True:
await asyncio.sleep(delay)
action_done = True
break
async def wait_for(action_to_wait):
print("wait for action to be done...")
while not action_to_wait:
await asyncio.sleep(0.1)
async def wait_for_action_to_be_done():
print("wait for action to be done...")
while not action_done:
await asyncio.sleep(0.1)
async def function_B():
await wait_for_action_to_be_done() # OK, works
# await wait_for(action_done) # KO, infinite while loop as the condition is never updated
print("performing next action...")
async def main():
await asyncio.gather(function_A(2), function_B())
asyncio.run(main())
[/code]