job_xx.py 2.5 KB

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