job_wxk.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import os
  2. import concurrent.futures
  3. import schedule
  4. import time
  5. import threading
  6. from common import Material
  7. # 控制读写速度的参数
  8. from video_rewriting.video_prep import getVideo
  9. from video_rewriting.video_processor import VideoProcessor
  10. MAX_BPS = 120 * 1024 * 1024 # 120MB/s
  11. MAX_WORKERS = os.cpu_count() * 2 # 线程池最大工作线程数量
  12. READ_WRITE_CHUNK_SIZE = 1024 * 1024 # 每次读写的块大小 (1MB)
  13. SLEEP_INTERVAL = READ_WRITE_CHUNK_SIZE / MAX_BPS # 控制每次读写的延迟时间
  14. # 全局锁,用于同步读写操作
  15. lock = threading.Lock()
  16. def video_task_start(data):
  17. mark = VideoProcessor.main(data)
  18. print(f"返回用户名{mark}")
  19. # data = Material.feishu_list()
  20. # video_task_start(data[0])
  21. def controlled_io_operation(data):
  22. with lock:
  23. start_time = time.time()
  24. time.sleep(SLEEP_INTERVAL)
  25. end_time = time.time()
  26. elapsed_time = end_time - start_time
  27. if elapsed_time < SLEEP_INTERVAL:
  28. time.sleep(SLEEP_INTERVAL - elapsed_time)
  29. video_task_start(data)
  30. def video_start():
  31. print("开始执行生成视频脚本.")
  32. data = Material.feishu_list()
  33. data = data[0]
  34. with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
  35. futures = {executor.submit(controlled_io_operation, data)}
  36. for future in concurrent.futures.as_completed(futures):
  37. try:
  38. future.result()
  39. print("处理结果: 成功")
  40. except concurrent.futures.TimeoutError:
  41. print("任务超时,已取消.")
  42. except Exception as e:
  43. print("处理任务时出现异常:", e)
  44. print("执行生成视频脚本结束.")
  45. video_start()
  46. schedule.every(6).hours.do(video_start)
  47. while True:
  48. schedule.run_pending()
  49. time.sleep(1)