video_job.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import os
  2. import concurrent.futures
  3. import re
  4. import schedule
  5. import time
  6. import threading
  7. from common import Material, Feishu
  8. # 控制读写速度的参数
  9. from video_rewriting.video_prep import getVideo
  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. # 记录今天已经返回的用户名
  17. today = []
  18. def video_task_start(data):
  19. global today
  20. user_data_mark = data["mark"]
  21. # 开始准备执行生成视频脚本
  22. if user_data_mark is not None and user_data_mark in today:
  23. print(f"视频脚本参数中的用户名 {user_data_mark} 今天已经返回过,不再启动线程。")
  24. return
  25. mark = getVideo.video_task(data)
  26. print(f"返回用户名{mark}")
  27. if mark:
  28. today.append(mark)
  29. # data = Material.feishu_list()
  30. # video_task_start(data[0])
  31. def controlled_io_operation(data):
  32. with lock:
  33. start_time = time.time()
  34. time.sleep(SLEEP_INTERVAL)
  35. end_time = time.time()
  36. elapsed_time = end_time - start_time
  37. if elapsed_time < SLEEP_INTERVAL:
  38. time.sleep(SLEEP_INTERVAL - elapsed_time)
  39. video_task_start(data)
  40. def video_start():
  41. print("开始执行生成视频脚本.")
  42. data = Material.feishu_list()
  43. with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
  44. futures = {executor.submit(controlled_io_operation, user_data): user_data for user_data in data}
  45. for future in concurrent.futures.as_completed(futures):
  46. try:
  47. future.result()
  48. print("处理结果: 成功")
  49. except concurrent.futures.TimeoutError:
  50. print("任务超时,已取消.")
  51. except Exception as e:
  52. print("处理任务时出现异常:", e)
  53. print("执行生成视频脚本结束.")
  54. def usernames_today():
  55. today.clear()
  56. print("today 已清空")
  57. video_start()
  58. # 定时任务设置
  59. schedule.every().day.at("01:00").do(usernames_today)
  60. schedule.every(12).hours.do(video_start)
  61. while True:
  62. schedule.run_pending()
  63. time.sleep(1)