1

I'll try to use gunicorn in my django project and faced with curious problem. Myproject sometimes catch Internal Server Error. I also added gunicorn errorlog, nginx config, and gunicorn run command. Problem is not in a circular import. When I run project like a ./manage.py runserver everything ok. That can happen on any page of the app and any moment. Can anyone give me a clue why that's happens?

gunicorn error log:

Traceback (most recent call last):
      File "/home/project/virt_env/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 93, in handle
        self.handle_request(listener, req, client, addr)
      File "/home/project/virt_env/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 134, in handle_request
        respiter = self.wsgi(environ, resp.start_response)
      File "/home/project/virt_env/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 206, in __call__
        response = self.get_response(request)
      File "/home/project/virt_env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 196, in get_response
        response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
      File "/home/project/virt_env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 234, in handle_uncaught_exception
        if resolver.urlconf_module is None:
      File "/home/project/virt_env/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 356, in urlconf_module
        self._urlconf_module = import_module(self.urlconf_name)
      File "/home/project/virt_env/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
        __import__(name)
      File "/home/project/project/urls.py", line 12, in <module>
        admin.autodiscover()
      File "/home/project/virt_env/local/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 29, in autodiscover
        import_module('%s.admin' % app)
      File "/home/project/virt_env/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
        __import__(name)
      File "/home/project/virt_env/local/lib/python2.7/site-packages/djcelery/admin.py", line 295, in <module>
        class PeriodicTaskAdmin(admin.ModelAdmin):
      File "/home/project/virt_env/local/lib/python2.7/site-packages/djcelery/admin.py", line 297, in PeriodicTaskAdmin
        form = periodic_task_form()
      File "/home/project/virt_env/local/lib/python2.7/site-packages/djcelery/admin.py", line 251, in periodic_task_form
        current_app.loader.import_default_modules()
      File "/home/project/virt_env/local/lib/python2.7/site-packages/djcelery/loaders.py", line 141, in import_default_modules
        self.autodiscover()
      File "/home/project/virt_env/local/lib/python2.7/site-packages/djcelery/loaders.py", line 144, in autodiscover
        self.task_modules.update(mod.__name__ for mod in autodiscover() or ())
      File "/home/project/virt_env/local/lib/python2.7/site-packages/djcelery/loaders.py", line 179, in autodiscover
        for app in settings.INSTALLED_APPS])
      File "/home/project/virt_env/local/lib/python2.7/site-packages/djcelery/loaders.py", line 203, in find_related_module
        return importlib.import_module('{0}.{1}'.format(app, related_name))
      File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
        __import__(name)
      File "/home/project/lab/tasks.py", line 8, in <module>
        from lab.sender2 import sender
      File "/home/project/lab/sender2.py", line 13, in <module>
        from lab.models import LabOrder, OutputLog
      File "/home/project/lab/models.py", line 977, in <module>
        from lab import signals
      File "/home/project/lab/signals.py", line 8, in <module>
        from lab import tasks
    ImportError: cannot import name tasks

nginx config:

server {
    listen   8001 default_server;

    access_log  /var/log/project/access.log;
    error_log   /var/log/project/error.log;

    location / {
        proxy_pass http://127.0.0.1:8080/;
        proxy_connect_timeout 300s;
        proxy_read_timeout 300s;
        client_max_body_size  10m;
    }

    location /static/media {
             alias /home/project/media;
         }

    location /static {
        alias /home/project/static;
    }

}

command for starting project:

gunicorn  project.wsgi --bind=127.0.0.1:8080 --workers=10 --pid=/var/log/project/gunicorn.pid --log-file /var/log/project/gunicorn.log

Look's like only one worker works fine and the others does not have correct python path gunicorn_access_log

<12885> 127.0.0.1 - [30/Jan/2015:16:16:11 +0300] "1"  "GET / HTTP/1.0" 200 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36"
<12885> 127.0.0.1 - [30/Jan/2015:16:16:15 +0300] "0"  "GET /login/?next=/ HTTP/1.0" 200 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0"
<12885> 127.0.0.1 - [30/Jan/2015:16:16:16 +0300] "0"  "GET /login/?next=/ HTTP/1.0" 200 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0"
<12885> 127.0.0.1 - [30/Jan/2015:16:16:16 +0300] "0"  "GET /login/?next=/ HTTP/1.0" 200 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0"
<12882> 127.0.0.1 - [30/Jan/2015:16:16:22 +0300] "0"  "GET /login/?next=/ HTTP/1.0" 500 "-" "-"
<12877> 127.0.0.1 - [30/Jan/2015:16:16:33 +0300] "0"  "GET /login/?next=/ HTTP/1.0" 500 "-" "-"
<12883> 127.0.0.1 - [30/Jan/2015:16:16:39 +0300] "0"  "GET /login/?next=/ HTTP/1.0" 500 "-" "-"
<12885> 127.0.0.1 - [30/Jan/2015:16:16:40 +0300] "0"  "GET /login/?next=/ HTTP/1.0" 200 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0"
<12885> 127.0.0.1 - [30/Jan/2015:16:16:44 +0300] "0"  "GET /login/?next=/ HTTP/1.0" 200 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0"
<12879> 127.0.0.1 - [30/Jan/2015:16:16:49 +0300] "0"  "GET /login/?next=/ HTTP/1.0" 500 "-" "-"
<12879> 127.0.0.1 - [30/Jan/2015:16:16:55 +0300] "0"  "GET /login/?next=/ HTTP/1.0" 500 "-" "-"
<12883> 127.0.0.1 - [30/Jan/2015:16:16:58 +0300] "0"  "GET /login/?next=/ HTTP/1.0" 500 "-" "-"
<12885> 127.0.0.1 - [30/Jan/2015:16:16:59 +0300] "0"  "GET /login/?next=/ HTTP/1.0" 200 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0"
<12884> 127.0.0.1 - [30/Jan/2015:16:17:04 +0300] "0"  "GET /login/?next=/ HTTP/1.0" 500 "-" "-"
<12881> 127.0.0.1 - [30/Jan/2015:16:17:08 +0300] "0"  "GET /login/?next=/ HTTP/1.0" 500 "-" "-"

Hope this information helps.

dyus
  • 91
  • 5
  • It looks very circular to me. Have you seen [this question](http://stackoverflow.com/questions/17845366/importerror-cannot-import-name?lq=1) and [this one](http://stackoverflow.com/questions/1556387/circular-import-dependency-in-python)? – André Laszlo Jan 30 '15 at 13:04
  • I do not think so. I refresh page 10 times and 9 times everything goes fine – dyus Jan 30 '15 at 13:11
  • I add to the question gunicorn access log, looks like only one worker can process requests and the others can not. Can someone explain how that can be? – dyus Jan 30 '15 at 13:22
  • Isn't the 9 times out of 10 consistent with the periodic task part of your question? – André Laszlo Jan 30 '15 at 13:31
  • I mean how it can be circular import? When i run project and go to the login page for example, refresh login page several times, then sometimes i have error sometimes not. So yes part of the question is how i can fix this issues. – dyus Jan 30 '15 at 13:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69894/discussion-between-dyus-and-andre-laszlo). – dyus Jan 30 '15 at 14:05
  • 1
    The problem with this issue is that the failed requests are using HTTP 1.0, they are not sending the"host" header nor the user agent, although the user agent is not an issue here. – Paco Valdez Jul 25 '16 at 17:49

0 Answers0