Python 无法使用结构启动gunicorn
我的代码位于远程服务器的Python 无法使用结构启动gunicorn,python,fabric,gunicorn,Python,Fabric,Gunicorn,我的代码位于远程服务器的/home/ubuntu/api中。WSGI对象名为app,它出现在/home/ubuntu/api/api.py中。我的gunicorn conf文件名为gunicorn.conf.py,位于/home/ubuntu/api mygunicorn.conf.py import multiprocessing bind = "127.0.0.1:8000" workers = multiprocessing.cpu_count() * 2 + 1 backlog = 2
/home/ubuntu/api
中。WSGI对象名为app
,它出现在/home/ubuntu/api/api.py
中。我的gunicorn conf文件名为gunicorn.conf.py
,位于/home/ubuntu/api
mygunicorn.conf.py
import multiprocessing
bind = "127.0.0.1:8000"
workers = multiprocessing.cpu_count() * 2 + 1
backlog = 2048
worker_class = 'gevent'
daemon = True
debug = True
loglevel = 'debug'
accesslog = '/mnt/log/gunicorn_access.log'
errorlog = '/mnt/log/gunicorn_error.log'
max_requests = 1000
graceful_timeout = 20
我正试图通过fabric在服务器上远程启动gunicorn。我的结构代码如下所示
@roles('stag_api')
def run_server():
with cd('/home/ubuntu/api'):
sudo('gunicorn -c gunicorn.conf.py api:app')
if [ "$1" = "start" ]; then
echo 'starting'
gunicorn -c /home/ubuntu/api/gunicorn.conf.py api:app
fi
if [ "$1" = "stop" ]; then
echo 'stopping'
pkill gunicorn
fi
if [ "$1" = "restart" ]; then
echo 'restarting'
pkill gunicorn
gunicorn -c /home/ubuntu/api/gunicorn.conf.py api:app
fi
现在fabric没有显示任何错误,但gunicorn没有启动
所以我在/home/ubuntu/api
中创建了\uuuu init\uuuu.py
,使其成为一个包。我在\uuuu init\uuuu.py
文件中写了这个
from api import app
这使得WSGIapp
在包的名称空间中可用。然后我把结构代码改成了这个
@roles('stag_api')
def run_server():
sudo('gunicorn -c /home/ubuntu/api/gunicorn.conf.py api:app')
即使现在fabric也不会显示任何错误,但gunicorn不会启动
因此,我创建了一个名为server
的shell脚本,其代码如下所示
@roles('stag_api')
def run_server():
with cd('/home/ubuntu/api'):
sudo('gunicorn -c gunicorn.conf.py api:app')
if [ "$1" = "start" ]; then
echo 'starting'
gunicorn -c /home/ubuntu/api/gunicorn.conf.py api:app
fi
if [ "$1" = "stop" ]; then
echo 'stopping'
pkill gunicorn
fi
if [ "$1" = "restart" ]; then
echo 'restarting'
pkill gunicorn
gunicorn -c /home/ubuntu/api/gunicorn.conf.py api:app
fi
我将这个shell脚本放在/home/ubuntu/api
现在我的结构代码如下所示
@roles('stag_api')
def stag_server(action='restart'):
if action not in ['start', 'stop', 'restart']:
print 'not a valid action. valid actions are start, stop and restart'
return
sudo('./server %s' % action)
现在,当我尝试通过fabric启动服务器时,它会打印启动
,因此shell脚本正在执行,如果到达块,则执行,但我仍然无法通过fabric启动服务器
但是如果我ssh到服务器并执行sudo./server start
,gunicorn就会启动
有人能解释一下我做错了什么吗?这很可能与以下两个常见问题有关:
尝试使用以下选项全局设置pty False:
from fabric.api import env
env.always_use_pty = False
或使用以下命令将该命令的pty设置为False:
run('run unicorn command etc...' pty=False)
请参阅中的初始化脚本部分:
提供错误日志,please@amezhenin没有错误。它只是没有启动Gunicorn你明白了吗?你找到解决方案了吗?我处于同样的情况,使用pyt=False也不起作用。请注意,不鼓励只使用链接的答案(链接往往会随着时间的推移而断开)。请考虑编辑你的答案并添加一个简介。谢谢。添加pty=False
有效。因此,我将代码更改为sudo('./服务器%s'%action,pty=False)
,现在它可以成功启动guncorn。请让你的回答更具描述性。@lovesh谢谢!原始答案中的链接不再有效。我发现最接近的是,但它没有在初始化脚本或后台进程的上下文中讨论它。修复了链接,出于某种原因,它们被移出了docs.fabfile.org。很抱歉给你带来了困惑。