etianen / aiohttp-wsgi

WSGI adapter for aiohttp.
https://aiohttp-wsgi.readthedocs.io
BSD 3-Clause "New" or "Revised" License
232 stars 20 forks source link

Runtime error using Django #24

Closed Keda87 closed 5 years ago

Keda87 commented 5 years ago

Hi, I have Django project using python 3.7.0 Due to minimum example in the documentation, I'm trying to follow this docs and edit my wsgi like this.

import os

from aiohttp import web
from aiohttp_wsgi import WSGIHandler
from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

application = get_wsgi_application()

wsgi_handler = WSGIHandler(application)
app = web.Application()
app.router.add_route('*', '/{path_info:.*}', wsgi_handler)

But when I run ./manage.py runserver I got these error

Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x106324158>
Traceback (most recent call last):
  File "/Users/adiyatmubarak/.local/share/virtualenvs/rumah-dongeng-pelangi-api-avzb_er2/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/Users/adiyatmubarak/.local/share/virtualenvs/rumah-dongeng-pelangi-api-avzb_er2/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 137, in inner_run
    handler = self.get_handler(*args, **options)
  File "/Users/adiyatmubarak/.local/share/virtualenvs/rumah-dongeng-pelangi-api-avzb_er2/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
    handler = super().get_handler(*args, **options)
  File "/Users/adiyatmubarak/.local/share/virtualenvs/rumah-dongeng-pelangi-api-avzb_er2/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 64, in get_handler
    return get_internal_wsgi_application()
  File "/Users/adiyatmubarak/.local/share/virtualenvs/rumah-dongeng-pelangi-api-avzb_er2/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 45, in get_internal_wsgi_application
    return import_string(app_path)
  File "/Users/adiyatmubarak/.local/share/virtualenvs/rumah-dongeng-pelangi-api-avzb_er2/lib/python3.7/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "/Users/adiyatmubarak/.local/share/virtualenvs/rumah-dongeng-pelangi-api-avzb_er2/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/adiyatmubarak/Documents/Koding/Dewe/rumah-dongeng-pelangi-api/src/rumah_dongeng_pelangi/wsgi.py", line 20, in <module>
    wsgi_handler = WSGIHandler(application)
  File "/Users/adiyatmubarak/.local/share/virtualenvs/rumah-dongeng-pelangi-api-avzb_er2/lib/python3.7/site-packages/aiohttp_wsgi/wsgi.py", line 178, in __init__
    self._loop = loop or asyncio.get_event_loop()
  File "/Users/adiyatmubarak/.pyenv/versions/3.7.0/lib/python3.7/asyncio/events.py", line 644, in get_event_loop
    % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Dummy-1'.
jordic commented 5 years ago

The problem is that you are trying to run the threaded django server, but with this package you have to run inside the aiohttp server, you miss the: web.run_app(app) at the end of the file, and later you can just run it with: python app.py