job_cover_method.py 908 B

123456789101112131415161718192021222324252627282930
  1. import asyncio
  2. import time
  3. from common.redis import get_pq_id
  4. from video_cover_method.cover_method import CoverMethod
  5. semaphore = asyncio.Semaphore(10) # 限制并发为 10 个
  6. async def get_video_id():
  7. while True:
  8. video_id = get_pq_id()
  9. if video_id:
  10. return video_id
  11. else:
  12. print("没有获取待更改封面的视频ID,等待10秒")
  13. await asyncio.sleep(5)
  14. async def process_video_cover(video_id):
  15. async with semaphore: # 限制并发
  16. try:
  17. await CoverMethod.cover_method(int(video_id))
  18. except Exception as e:
  19. print("处理任务时出现异常:", e)
  20. async def video_cover_task_start():
  21. while True:
  22. video_id = await get_video_id()
  23. asyncio.create_task(process_video_cover(video_id)) # 创建异步任务
  24. if __name__ == '__main__':
  25. asyncio.run(video_cover_task_start())