python 异步处理 运行 两个无限函数时遇到问题

python asyncio having trouble with running two infinite functions asynchronously

所以我一直在尝试同时 运行 两个功能,但一个似乎永远无法工作,除非我停止另一个。第一个功能旨在每 30 秒发送一封电子邮件(功能经过自身测试并且有效)第二个功能是每 5 秒打印一个简单的语句。因此,在我看到每 6 个“Hello Worlds”语句后,我应该会收到一封电子邮件。但是,除非我在 运行 函数中实现一些东西来停止它,比如“10 秒后结束”,否则我永远不会收到一封电子邮件。我希望这两个功能都能无限继续(如果可能的话不会崩溃)。感谢任何帮助。

async def timer():
    now = time.time()
    end = now + 30
    #sendmail(name, filepath + "\" + name, receiver)
    while True:
        if time.time() >= end:
            sendmail(name, filepath + "\" + name, receiver)
            now = time.time()
            end = now + 30

async def runs():
    while True:
        print("Hello World")
        time.sleep(5)


loop = asyncio.get_event_loop()
loop.create_task(runs())
loop.create_task(timer())
loop.run_forever()

此外,如果有人能够使用多处理或线程模块完成任务,我很想知道它是如何完成的,因为我已经尝试过但都失败了。 我尝试过的示例:

t1 = threading.Thread(target=timer)
t2 = threading.Thread(target=runs)
t1.start()
t2.start()

Python 的 async 协程用于 合作 并发。这意味着协程必须主动允许其他人运行。对于简单的情况,使用 await asyncio.sleep 暂停当前协程和 运行 其他协程。

async def timer():
    while True:
        await asyncio.sleep(30)
        sendmail(name, filepath + "\" + name, receiver)

async def runs():
    while True:
        print("Hello World")
        await asyncio.sleep(5)

async def main():
    await asyncio.gather(timer(), runs())

asyncio.run(main())

值得注意的是,不要使用 time.sleep – 这会阻塞整个线程,这意味着当前协程 以及事件循环和所有其他协程 .
同样,避免任何具有显着 运行time 的同步代码 - asyncio 不能在同步代码 运行s 时切换到其他协程。如果需要,在线程中使用 asyncio 助手来 运行 同步代码,例如asyncio.to_thread or loop.run_in_executor.

async def timer():
    next_run = time.time()
    while True:
        # run blocking function in thread to keep event-loop free
        await asyncio.to_thread(sendmail, name, filepath + "\" + name, receiver)
        # pause to run approx every 30 seconds
        await asyncio.sleep(next_run - time.time())
        next_run += 30

你可以使用 await asycio.sleep()进行异步等待

import asyncio


async def timer():
    while True:
        
        print("email sent")
        await asyncio.sleep(30)


async def runs():
    while True:
        print("Hello World")
        await asyncio.sleep(5)

loop = asyncio.get_event_loop()
loop.create_task(timer())
loop.create_task(runs())
loop.run_forever()