job_dd_sph.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. def controlled_io_operation(data):
  19. with lock:
  20. start_time = time.time()
  21. time.sleep(SLEEP_INTERVAL)
  22. end_time = time.time()
  23. elapsed_time = end_time - start_time
  24. if elapsed_time < SLEEP_INTERVAL:
  25. time.sleep(SLEEP_INTERVAL - elapsed_time)
  26. video_task_start(data)
  27. def video_start():
  28. print("开始执行生成视频脚本.")
  29. data = Material.feishu_list()
  30. data = data[12]
  31. with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
  32. futures = {executor.submit(controlled_io_operation, data)}
  33. for future in concurrent.futures.as_completed(futures):
  34. try:
  35. future.result()
  36. print("处理结果: 成功")
  37. except concurrent.futures.TimeoutError:
  38. print("任务超时,已取消.")
  39. except Exception as e:
  40. print("处理任务时出现异常:", e)
  41. print("执行生成视频脚本结束.")
  42. video_start()
  43. schedule.every(1).hours.do(video_start)
  44. # schedule.every(20).minutes.do(video_start)
  45. while True:
  46. schedule.run_pending()
  47. time.sleep(1)