3

您好我正在尝试将我的 django 1.4.1 应用程序与 Gunicorn 0.14.6 集成。我像这样从命令行启动 gunicorn 服务器 -

gunicorn -c /home/code/gunicorn_config.py

我得到了这个回溯-

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 459, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 99, in init_process
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/base.py", line 101, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 24, in load
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/util.py", line 292, in import_app
    app = eval(obj, mod.__dict__)
  File "<string>", line 1, in <module>
NameError: name 'application' is not defined

我哪里错了?这是什么application变量以及我需要在哪里修改它?

另外,由于我使用的是 Django1.4.1 wsgi.py,我的项目中已经有一个文件,我需要更改它吗?

更新:这是我的gunicorn_config.py文件内容 -

import os
import sys
import multiprocessing

def app_path():
    sys.path.append('/home/code/po/')
    sys.path.append('/home/code/po/ball/')
    return

def num_cpus():
    cpus = 0
    try:
        cpus = os.sysconf("SC_NPROCESSORS_ONLN")
    except:
        cpus =  multiprocessing.cpu_count()

    if cpus: return cpus
    else: return 3

#defining the behavior of gunicorn
app_path()

bind      = "127.0.0.1:8080"
workers   = num_cpus()*2 + 1
debug     = True
daemon    = False
accesslog = '/home/code/logs/guni_access.log'
errorlog  = '/home/code/logs/guni_error.log'
loglevel  = 'debug'
django_settings  = '/home/code/po/po/'
pythonpath       = '/home/code/po/'

@moopet - 我什至不认为该wsgi.py文件被调用,我如何让 gunicorn 选择该文件?

4

2 回答 2

2

django_settings的不正确。django_settings应该采用可从您设置的 Python 路径导入的 python 模块导入的形式。所以

pythonpath = '/home/code/po'
django_settings = 'po.settings'

更详细地说,applicationgunicorn 将尝试从您提供的 Python 模块导入默认变量(应该是 WSGI 应用程序对象)。

所以换一种方式考虑。假设您正在尝试运行一个简单的 Flask wsgi 应用程序。实际的 WSGI 应用程序被定义为并存application在于/home/code/views.py. 然后以下将手动开始使用 gunicorn 提供服务

export PYTHONPATH=/home/code
gunicorn -w 2 views:application

所以视图模块内的变量应用程序。您可以阅读有关Django 如何为您提供应用程序对象的信息。

您可能需要将 gunicorn 指向po.wsgi模块本身。从目前提供的信息来看,有点难以判断。如果该模块被正确创建,它应该包含一个名为application

于 2012-09-18T17:48:38.667 回答
0

Check if another package you have installed already contains a file called wsgi.py. (gevent does.) If so, likely the wrong wsgi.py file is being loaded. Try renaming your wsgi.py file to something else (e.g. app_wsgi.py) and add run your app using

gunicorn -c /home/code/gunicorn_config.py app_wsgi
于 2013-03-09T17:33:56.383 回答