123456789101112131415161718192021222324252627282930 |
- import asyncio
- import time
- from common.redis import get_pq_id
- from video_cover_method.cover_method import CoverMethod
- semaphore = asyncio.Semaphore(10) # 限制并发为 10 个
- async def get_video_id():
- while True:
- video_id = get_pq_id()
- if video_id:
- return video_id
- else:
- print("没有获取待更改封面的视频ID,等待10秒")
- await asyncio.sleep(5)
- async def process_video_cover(video_id):
- async with semaphore: # 限制并发
- try:
- await CoverMethod.cover_method(int(video_id))
- except Exception as e:
- print("处理任务时出现异常:", e)
- async def video_cover_task_start():
- while True:
- video_id = await get_video_id()
- asyncio.create_task(process_video_cover(video_id)) # 创建异步任务
- if __name__ == '__main__':
- asyncio.run(video_cover_task_start())
|