job_sph.py 1.8 KB

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