我希望能够将变量或其他内容发送到特定的异步任务(或类似任务),以便它可以跳过休眠部分。这是我正在使用的结构:
async def someFunction():
x = 'something'
variable_i_need = False
asyncio.create_task(someLoop(x, variable_i_need))
async def someLoop(x, variable_i_need):
while True:
print(x)
for i in range(60):
if variable_i_need:
break
await asyncio.sleep(1)
async def someOtherFunction():
variable_i_need = True
#Send this to specific task running someLoop()
有什么方法可以做到这一点或类似的事情,我可以使用与x数量的其他任务相同的循环来更新一个任务。我可以使用全局变量来完成此操作,但它会更新所有任务,而不仅仅是一个任务。
最佳答案
我会使用全局字典,
假设变量_i_need={},其中 key= 任务名称且值为 True 或 False
然后从某个循环我将检查
如果variable_i_need{x}:中断
然后使用:
async def someOtherFunction(x):
variable_i_need{x} = True
关于python - 有没有办法将变量发送到特定任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59965249/