agc_job_main.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import os
  2. import concurrent.futures
  3. import schedule
  4. import time
  5. import threading
  6. from common import Material
  7. from video_agc.agc_video import AGC
  8. # 控制读写速度的参数
  9. MAX_BPS = 1 * 1024 * 1024 # 120MB/s
  10. MAX_WORKERS = os.cpu_count() * 5 # 线程池最大工作线程数量
  11. READ_WRITE_CHUNK_SIZE = 512 * 1024 # 每次读写的块大小 (1MB)
  12. SLEEP_INTERVAL = READ_WRITE_CHUNK_SIZE / MAX_BPS # 控制每次读写的延迟时间
  13. # 全局锁,用于同步读写操作
  14. lock = threading.Lock()
  15. def controlled_io_operation(platform, data):
  16. with lock:
  17. start_time = time.time()
  18. time.sleep(SLEEP_INTERVAL)
  19. end_time = time.time()
  20. elapsed_time = end_time - start_time
  21. if elapsed_time < SLEEP_INTERVAL:
  22. time.sleep(SLEEP_INTERVAL - elapsed_time)
  23. if platform == "gs":
  24. AGC.video(data, "跟随")
  25. elif platform == "cg":
  26. AGC.video(data, "常规")
  27. elif platform == "bk":
  28. AGC.video(data, "爆款")
  29. def video_start(platform):
  30. print("开始执行生成视频脚本.")
  31. if platform == "cg":
  32. data = Material.feishu_list()
  33. elif platform == "gs":
  34. data = Material.feishu_gs_list()
  35. elif platform == "bk":
  36. data = Material.feishu_bk_list()
  37. with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
  38. futures = {executor.submit(controlled_io_operation, platform, user_data): user_data for user_data in data}
  39. for future in concurrent.futures.as_completed(futures):
  40. try:
  41. future.result()
  42. print("处理结果: 成功")
  43. except concurrent.futures.TimeoutError:
  44. print("任务超时,已取消.")
  45. except Exception as e:
  46. print("处理任务时出现异常:", e)
  47. print("执行生成视频脚本结束.")
  48. schedule.every().day.at("04:10").do(video_start, "cg")
  49. schedule.every().day.at("19:40").do(video_start, "gs")
  50. schedule.every().day.at("00:20").do(video_start, "bk")
  51. if __name__ == "__main__":
  52. # video_start("cg")
  53. while True:
  54. try:
  55. schedule.run_pending()
  56. except Exception as e:
  57. print("执行调度任务时出现异常:", e)
  58. time.sleep(1)