瀏覽代碼

时光相册-视频生成 设置虚拟显示服务

linhaohong 11 月之前
父節點
當前提交
8da492accb
共有 1 個文件被更改,包括 22 次插入5 次删除
  1. 22 5
      CeleryTask/TimeAlbumTask.py

+ 22 - 5
CeleryTask/TimeAlbumTask.py

@@ -9,16 +9,33 @@ import django
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'Ansjer.cn_config.test_settings')  # 确保替换为实际的 settings 模块路径
 django.setup()
 
-@shared_task
+
 def start_xvfb():
-    # 启动 Xvfb
-    subprocess.Popen(['Xvfb', ':99', '-screen', '0', '1024x768x16'])
-    os.environ['DISPLAY'] = ':99'
+    try:
+        # 检查 Xvfb 是否已经在运行
+        output = subprocess.check_output("pgrep -f Xvfb", shell=True)
+        if output:
+            LOGGER.info('虚拟显示服务已在运行')
+            os.environ['DISPLAY'] = ':99'
+        else:
+            # 启动虚拟显示服务
+            process = subprocess.Popen(['Xvfb', ':99', '-screen', '0', '1024x768x16'])
+            LOGGER.info('虚拟显示服务启动成功,进程ID: %s', process.pid)
+            os.environ['DISPLAY'] = ':99'
+    except subprocess.CalledProcessError:
+        # 如果没有运行,启动 Xvfb
+        process = subprocess.Popen(['Xvfb', ':99', '-screen', '0', '1024x768x16'])
+        LOGGER.info('虚拟显示服务启动成功,进程ID: %s', process.pid)
+        os.environ['DISPLAY'] = ':99'
+    except Exception as e:
+        LOGGER.error('启动虚拟显示服务失败: %s', e)
+
 
 @shared_task
 def generate_video(image_files, output_path):
     LOGGER.info('start开始视频生成任务')
-    start_xvfb.delay()  # 确保 Xvfb 已经启动
+    # 启动虚拟显示服务
+    start_xvfb()
     try:
         video_files = []
         music_path = "/web/test/ASJServer/static/temp/music.mp3"