恢复main的并发
This commit is contained in:
34
main.py
34
main.py
@@ -74,17 +74,41 @@ def execute_task(tid, task_type, **kwargs):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
# 创建任务队列,最大容量为3
|
||||||
|
task_queue = Queue(maxsize=2)
|
||||||
|
# 存储正在运行的任务
|
||||||
|
running_tasks = set()
|
||||||
|
|
||||||
|
# 创建线程池执行任务(2个工作线程)
|
||||||
|
with ThreadPoolExecutor(max_workers=2) as executor:
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
# 清理已完成的任务
|
||||||
|
running_tasks = {task for task in running_tasks if not task.done()}
|
||||||
|
|
||||||
|
# 如果队列未满,尝试获取新任务
|
||||||
|
if len(running_tasks) < 2:
|
||||||
task = get_task()
|
task = get_task()
|
||||||
if task is None:
|
if task:
|
||||||
|
logger.info(f"收到任务: {task['id']} - {task['task_type']}")
|
||||||
|
task_data = task['data']
|
||||||
|
task_data['tid'] = task['id']
|
||||||
|
task_data['task_type'] = task['task_type']
|
||||||
|
# 提交任务到线程池并保存future对象
|
||||||
|
future = executor.submit(execute_task, **task_data)
|
||||||
|
running_tasks.add(future)
|
||||||
|
else:
|
||||||
|
logger.info("无更多任务")
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
continue
|
else:
|
||||||
task['data']['tid'] = task['id']
|
# 达到最大并发数时等待
|
||||||
task['data']['task_type'] = task['task_type']
|
logger.info("等待任务完成")
|
||||||
execute_task(**task['data'])
|
time.sleep(1)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
logger.error(f'Main Error: {e}')
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user