gpt4 book ai didi

python - 为什么我的 Python 子进程在由 supervisord 管理时会出错?

转载 作者:行者123 更新时间:2023-11-28 19:23:42 30 4
gpt4 key购买 nike

我编写了一个用于屏幕捕获的小型 API - 一个使用 subprocess.Popen 将捕获请求传递给 CasperJS 的 flask 应用

在 Mac 上进行开发,当我在我的生产服务器 Ubuntu 13.04 上的 shell 中运行服务器时 - 一切正常。

但是,当我使用 supervisord 管理服务器时,子进程调用返回一个错误,即 CasperJS 找不到 PhantomJS(Casper 在 Phantom 上运行)。

抛出的错误是:

致命:[Errno 2] 没有那个文件或目录;你安装了 phantomjs 吗?

代码全部开源。

这是子进程调用:

https://github.com/pwalsh/moment/blob/master/moment/models.py#L215

这里是服务器的 supervisor conf 文件(我用 Fabric 生成的实际文件,但应该很清楚):

https://github.com/pwalsh/moment/blob/master/fabfile/templates.py#L56

系统上只有两个用户 - root 和我的应用程序的用户。当我以这些用户身份登录机器时,我可以成功运行开发服务器,并且可以成功运行 PhantomJS 和 CasperJS。

为什么我的子进程使用 supervisord 出错?

编辑:添加代码+堆栈跟踪

gunicorn 服务器的 Supervisord conf:

; Generated via Fabric on 2013-08-18 23:05:50.928087
; gunicorn configuration for Moment
[program:moment-gunicorn]

command=/srv/environments/moment/bin/gunicorn moment:app --bind 127.0.0.1:9000 --workers 4 --timeout 30 --access-logfile /srv/logs/moment_gunicorn_access.log --error-logfile /srv/logs/moment_gunicorn_error.log

environment=PATH="/srv/environments/moment/bin"
directory=/srv/projects/moment
user=moment
autostart=true
autorestart=true

将数据发送到 CasperJS/PhantomJS 子进程的代码。它是一个类的方法,完整code is here :

def capture(self):

filename = '{key}.{format}'.format(key=self.get_key().lstrip(self.prefix),
format=self.arguments['format'])

image = os.path.join(conf.CAPTURES_ROOT, filename)

params = [conf.CASPER, conf.CAPTURE_SCRIPT, self.arguments['url'],
image, self.arguments['viewport'], self.arguments['target']]

casper = subprocess.Popen(params, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

casper_output, casper_errors = casper.communicate()

logging.info(casper_output)
logging.info(casper_errors)
logging.info(casper.returncode)

# Here we are relying on convention:
# If success, subprocess.returncode == 0
# This could be fragile, need to investigate.
if casper.returncode:

raise Exception(casper_errors)

else:

return image

回溯:

WARNING:root:Fatal: [Errno 2] No such file or directory; did you install phantomjs?
WARNING:root:
WARNING:root:1
ERROR:moment:Exception on /capture/ [GET]
Traceback (most recent call last):
File "/srv/environments/moment/local/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/srv/environments/moment/local/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/srv/environments/moment/local/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/srv/environments/moment/local/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/srv/environments/moment/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/srv/projects/moment/moment/views.py", line 45, in get_capture
image = capture.capture()
File "/srv/projects/moment/moment/models.py", line 229, in capture
raise Exception(casper_errors)
Exception

注意:

  • 我在名为“moment”的虚拟环境中运行,并在名为“moment”的用户下运行。
  • 错误在 casper_output 变量中——前三个警告是我在启动子进程时记录的警告

最佳答案

尽管(出于您应该调查的原因)用户已从原始 moment 用户升级为 root,但这并不意味着该进程在您以 root 身份登录到 shell。

很可能您的路径只是在您的 supervisord.conf 中设置的路径,这就是为什么 phantomjs 似乎不存在的原因。

在某些用户数据库中没有查找环境;相反,它们是通过显式设置值(例如使用脚本)或从生成过程继承来构建的。在这种情况下,您继承自 supervisor,并将获得 supervisor 拥有的任何环境。如果 supervisor 由诸如 cron 之类的东西运行,则该环境将为空。

与 supervisor 相关的最佳实践是使用正确设置环境的包装脚本运行它,或者只是在 supervisord.conf 中显式设置所有内容。我通常推荐后者,除非你在一个被很多脚本使用的文件中有一组通用的环境修正(比如因为你想让它在 virtualenv 中运行)。

关于python - 为什么我的 Python 子进程在由 supervisord 管理时会出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18303279/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com