job_wxk.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import os
  2. import concurrent.futures
  3. import time
  4. import threading
  5. from common import Material
  6. from video_rewriting.video_processor import VideoProcessor
  7. # 控制读写速度的参数
  8. MAX_BPS = 120 * 1024 * 1024 # 120MB/s
  9. MAX_WORKERS = os.cpu_count() * 2 # 线程池最大工作线程数量
  10. READ_WRITE_CHUNK_SIZE = 1024 * 1024 # 每次读写的块大小 (1MB)
  11. SLEEP_INTERVAL = READ_WRITE_CHUNK_SIZE / MAX_BPS # 控制每次读写的延迟时间
  12. # 全局锁,用于同步读写操作
  13. lock = threading.Lock()
  14. def video_task_start(data):
  15. """处理视频任务,返回用户名并根据结果决定延迟时间"""
  16. try:
  17. mark = VideoProcessor.main(data)
  18. print(f"返回用户名: {mark}")
  19. time.sleep(10 if mark else 120) # 根据 mark 是否为空设置延迟
  20. return mark
  21. except Exception as e:
  22. print("处理任务时出现异常:", e)
  23. return None
  24. def controlled_io_operation(data):
  25. """同步控制IO操作并执行视频任务"""
  26. with lock:
  27. time.sleep(SLEEP_INTERVAL)
  28. return video_task_start(data)
  29. def video_start():
  30. """启动视频生成任务"""
  31. print("开始执行生成视频脚本.")
  32. data = Material.feishu_list()[0]
  33. with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
  34. futures = [executor.submit(controlled_io_operation, data) for _ in range(MAX_WORKERS)]
  35. for future in concurrent.futures.as_completed(futures):
  36. try:
  37. result = future.result()
  38. print(f"处理结果: 成功, 用户名: {result}")
  39. except Exception as e:
  40. print("处理任务时出现异常:", e)
  41. print("执行生成视频脚本结束.")
  42. while True:
  43. video_start()