简体   繁体   English

两个异步任务 - 一个依赖于 python 中的另一个

[英]two async tasks - one depends on another in python

I need to write a code where i need to to check in real time a status of some variable.我需要编写一个代码来实时检查某个变量的状态。 I decited to use asyncio to create two async def functions我决定使用asyncio创建两个 async def 函数

import asyncio

async def one():
    global flag
    flag = True
    while flag == True:
        await asyncio.sleep(0.2)
        print("Doing one")

async def two():
    await asyncio.sleep(2)
    global flag
    flag = False

async def main():
    tasks = []
    tasks.append(one())
    tasks.append(two())
    await asyncio.gather(*tasks)

loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(main())
finally:
    loop.close()
    print("Loop ended")

When loop starts, all tasks has been lauched and after 2 seconds def two() sets flag=False , which stops def one() .当循环开始时,所有任务都已启动,并且在 2 秒后def two()设置flag=False ,这将停止def one() It's good but i want def one() to perform while loop without await asyncio.sleep(0.2) becouse i dont want to have real live update so i set await asyncio.sleep(0.0) .这很好,但我希望def one()在没有await asyncio.sleep(0.2)的情况下执行 while 循环,因为我不想进行真正的实时更新,所以我设置await asyncio.sleep(0.0) Is it a good practice?这是一个好习惯吗?

Using a global variable is indeed bad practice.使用全局变量确实是不好的做法。 What you are looking for is asyncio 's primitives, specifically theasyncio.Event primitive.您正在寻找的是asyncio的原语,特别是asyncio.Event原语。 Here is what you are doing, but with asyncio.Event :这是您正在做的事情,但是使用asyncio.Event

import asyncio

async def one(event):
    while event.is_set() == False:
        await asyncio.sleep(0.5)
        print("Hello World!")

async def two(event):
    await asyncio.sleep(2)
    event.set()

async def main():
    event = asyncio.Event()
    await asyncio.gather(*[one(event), two(event)])

asyncio.run(main())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 两个异步任务一起工作。 一个挡住另一个 - two async tasks works together. One blocks another one 当一个依赖另一个时,如何在python中同时完成两组任务? - How can I simultaneously complete two sets of tasks in python when one depends on the other? Python optparse,具有一个选项取决于另一个 - Python optparse, having one option depends on another Python asyncio有两项任务,只有一项正在运行 - Python asyncio two tasks and only one is running Python +机械化异步任务 - Python + Mechanize Async Tasks 当一种方法依赖于另一种方法的输出时设计Python类 - Designing a Python class when one method depends on the output of another method python中的约束优化,其中一个变量依赖于另一个变量 - Constrained optimization in python where one variable depends on another variable 使用 Celery 和 RabbitMQ 作为代理与仅使用 RabbitMQ + Pika 进行异步任务,使用一个优于另一个的优势 - Using Celery with RabbitMQ as broker vs using just RabbitMQ + Pika for async tasks, advantages of using one over another 如何编写两个并发异步并行 function 并相互执行? - How to write two concurrent async parallel function and execute one with another? Python:复杂的列表推导,其中一个var依赖于另一个(测试中t的t为t [1]中的x为x) - Python: complex list comprehensions where one var depends on another (x for x in t[1] for t in tests)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM