简体   繁体   English

Django Gunicorn wsgi

[英]Django Gunicorn wsgi

Hi I am trying to integrate my django 1.4.1 app with Gunicorn 0.14.6. 嗨,我正在尝试将我的django 1.4.1应用程序与Gunicorn 0.14.6集成。 I start gunicorn server from command line like so - 我从命令行启动gunicorn服务器,如此 -

gunicorn -c /home/code/gunicorn_config.py

I get this traceback - 我得到这个追溯 -

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

Where am i going wrong? 我哪里错了? Whats this application variable & where do I need to modify this? 这个application变量是什么?我需要在哪里修改它?

Also since I am using Django1.4.1 I have a wsgi.py file already in my project, do I need to change that? 此外,因为我使用Django1.4.1我的项目中已经有一个wsgi.py文件,我是否需要更改它?

UPDATE: here is my gunicorn_config.py file contents - 更新:这是我的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 - i dont even think that wsgi.py file is called, how do i make gunicorn pick that file ? @moopet - 我甚至不认为wsgi.py文件被调用,我如何制作gunicorn选择该文件?

Your django_settings is incorrect. 你的django_settings不正确。 django_settings should be in the form of a python module import that is importable from the Python path you set. django_settings应该是python模块导入的形式,可以从你设置的Python路径导入。 So 所以

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

To elaborate a bit more, application is the default variable (which should be a WSGI application object) gunicorn will try and import from the Python module that you supply. 为了详细说明, application是默认变量(应该是WSGI应用程序对象),gunicorn将尝试从您提供的Python模块导入。

So to think about it another way. 所以以另一种方式思考它。 Say you were trying to run a simple Flask wsgi appication. 假设您正在尝试运行简单的Flask wsgi应用程序。 The actual WSGI app was defined as application and lived inside /home/code/views.py . 实际的WSGI应用程序被定义为application并且位于/home/code/views.py Then the following would manually start serving it with gunicorn 然后以下将手动开始用gunicorn服务它

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

So the variable application inside the views module. 所以视图模块中的变量应用程序。 You can read about how Django provides you with the application object . 您可以阅读Django如何为您提供应用程序对象

It might be that you need to point gunicorn at the po.wsgi module itself. 可能是你需要在po.wsgi模块本身指向gunicorn。 It's a little hard to tell from the information provided so far. 从目前为止提供的信息来看,这有点难以辨别。 If that module was created properly it should contain a variable called application 如果该模块是正确创建的,它应该包含一个名为application的变量

Check if another package you have installed already contains a file called wsgi.py. 检查您安装的另一个软件包是否已包含名为wsgi.py的文件。 (gevent does.) If so, likely the wrong wsgi.py file is being loaded. (gevent确实。)如果是这样,可能是加载了错误的wsgi.py文件。 Try renaming your wsgi.py file to something else (eg app_wsgi.py) and add run your app using 尝试将您的wsgi.py文件重命名为其他内容(例如app_wsgi.py)并添加运行您的应用程序

gunicorn -c /home/code/gunicorn_config.py app_wsgi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM