ffmpeg Python命令在PM2环境下只运行一次
ffmpeg Python command only runs once in PM2 environment
PM2 是 运行 作为 web
用户。 ffmpeg 使用 sudo apt install ffmpeg
本机安装到 Ubuntu 16.04 LTS。 Python 版本是 3.6。软件使用ffmpeg-python@0.1.17.
生成的应用程序没有产生任何错误。当 ffmpeg 代码第一次执行时,我们看到一个输出,并且 ffmpeg 进程按预期完成了任务。
所有后续请求在下一次 ffmpeg 执行时停止。无输出。 ffmpeg 进程中没有 return。没有错误。 PM2进程没有报错。应用程序日志在 ffmpeg 命令上停止,就好像挂起一样。
根本原因是什么?非常感谢任何帮助。
此外,PM2 在子进程(如 ffmpeg)上挂起的原因是什么?
代码如下:
class ImageHelper:
def __init__(self):
pass
@classmethod
def create_thumb_from_video_ffmpeg(cls, input_video_file_path,
output_image_path,
scale_width,
scale_height
):
"""
This function is used to create the thumb image
from a source video file.
We are using a python wrapper/library for FFMPEG
"""
try:
if Functions.get_attribute_env('ENVIRONMENT') == 'prod':
out, err = (
ffmpeg
.input(input_video_file_path, ss="00:00:00.001")
.filter('scale', scale_width, scale_height)
.output(output_image_path, vframes=1, loglevel='quiet')
.overwrite_output()
.run(capture_stdout=True)
)
print("We only see this once!")
else:
out, err = (
ffmpeg
.input(input_video_file_path, ss="00:00:00.001")
.filter('scale', scale_width, scale_height)
.output(output_image_path, vframes=1)
.overwrite_output()
.run(capture_stdout=True)
)
print("We only see this once!")
if err:
if Functions.get_attribute_env('ENVIRONMENT') != 'prod':
print('ffmpeg video thumb', err)
else:
Functions.logger_function(str(err))
raise Exception(err)
else:
return output_image_path
except Exception as e:
if Functions.get_attribute_env('ENVIRONMENT') != 'prod':
print('in thumb exception', e)
else:
Functions.logger_function(str(e))
raise Exception(e)
检查 ffmpeg
进程是否在 运行 时发出顺序请求。如果是,您可能希望确保该进程在第一次完成后关闭,以便它可以为顺序请求再次启动。
由 apache
或 nginx
产生的进程将限制它们执行的时间,并将自动终止。在这些情况下,您可能希望将脚本触发到 Web 进程池的 运行 外部 ,例如。类似于:
setsid /usr/bin/python3 my_script.py
对于任何有子流程问题的人来说,它的价值是什么...解决方案结果归因于做得不好的 .env 实现。当我们最终重新创建 .env 时,问题就消失了。实际上,我向我的团队推荐我们将 Anaconda 用于我们的 Python 环境,并且成功了。 :'D
PM2 是 运行 作为 web
用户。 ffmpeg 使用 sudo apt install ffmpeg
本机安装到 Ubuntu 16.04 LTS。 Python 版本是 3.6。软件使用ffmpeg-python@0.1.17.
生成的应用程序没有产生任何错误。当 ffmpeg 代码第一次执行时,我们看到一个输出,并且 ffmpeg 进程按预期完成了任务。
所有后续请求在下一次 ffmpeg 执行时停止。无输出。 ffmpeg 进程中没有 return。没有错误。 PM2进程没有报错。应用程序日志在 ffmpeg 命令上停止,就好像挂起一样。
根本原因是什么?非常感谢任何帮助。
此外,PM2 在子进程(如 ffmpeg)上挂起的原因是什么?
代码如下:
class ImageHelper:
def __init__(self):
pass
@classmethod
def create_thumb_from_video_ffmpeg(cls, input_video_file_path,
output_image_path,
scale_width,
scale_height
):
"""
This function is used to create the thumb image
from a source video file.
We are using a python wrapper/library for FFMPEG
"""
try:
if Functions.get_attribute_env('ENVIRONMENT') == 'prod':
out, err = (
ffmpeg
.input(input_video_file_path, ss="00:00:00.001")
.filter('scale', scale_width, scale_height)
.output(output_image_path, vframes=1, loglevel='quiet')
.overwrite_output()
.run(capture_stdout=True)
)
print("We only see this once!")
else:
out, err = (
ffmpeg
.input(input_video_file_path, ss="00:00:00.001")
.filter('scale', scale_width, scale_height)
.output(output_image_path, vframes=1)
.overwrite_output()
.run(capture_stdout=True)
)
print("We only see this once!")
if err:
if Functions.get_attribute_env('ENVIRONMENT') != 'prod':
print('ffmpeg video thumb', err)
else:
Functions.logger_function(str(err))
raise Exception(err)
else:
return output_image_path
except Exception as e:
if Functions.get_attribute_env('ENVIRONMENT') != 'prod':
print('in thumb exception', e)
else:
Functions.logger_function(str(e))
raise Exception(e)
检查 ffmpeg
进程是否在 运行 时发出顺序请求。如果是,您可能希望确保该进程在第一次完成后关闭,以便它可以为顺序请求再次启动。
由 apache
或 nginx
产生的进程将限制它们执行的时间,并将自动终止。在这些情况下,您可能希望将脚本触发到 Web 进程池的 运行 外部 ,例如。类似于:
setsid /usr/bin/python3 my_script.py
对于任何有子流程问题的人来说,它的价值是什么...解决方案结果归因于做得不好的 .env 实现。当我们最终重新创建 .env 时,问题就消失了。实际上,我向我的团队推荐我们将 Anaconda 用于我们的 Python 环境,并且成功了。 :'D