from eventlet import monkey_patch
monkey_patch()
import eventlet
import time
from loguru import logger
def run(task_id: int):
logger.debug(f'开始执行任务: {task_id}')
time.sleep(3)
logger.debug(f'开始执行任务: {task_id}')
eventlet.spawn_n(run,1)
eventlet.spawn_n(run,2)
写了上面的代码,但是有一个问题,就是运行后,程序就退出了,都没有执行 run
所以,如何保证任务都运行完了,才退出进程?
chatGPT 给的方法不行,不能运行
╰─➤ time python main.py
Traceback (most recent call last):
File "/Users/ponponon/Desktop/code/me/ideaboom/main.py", line 18, in <module>
eventlet.greenthread.waitall()
AttributeError: module 'eventlet.greenthread' has no attribute 'waitall'
python main.py 0.49s user 0.07s system 79% cpu 0.702 total