[Django]-SynchronousOnlyOperation error when upgrading from python 3.6 to python 3.7 using django channels

1👍

Finally got to the bottom of this. For us, we were using gevent and had this line in our manage.py

# manage.py

import gevent

gevent.patch_all()

For whatever reason, that does not play nicely with python 3.7+, Django channels, and Django (where python 3.6, Django channels, and Django worked fine).

We were lucky and were able to remove this from our codebase; we did not figure out how to fix the gevent patch.

1👍

So a summary of what I’ve tried:

  • First, I have a channels app that had no database at all, no contrib apps except staticfiles.
  • I added database, DRF and one app with a single model and list/detail views
  • Enabled the sync http consumer:
# dwtools.routing
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application

import romanize.routing

application = ProtocolTypeRouter(
    {
        "websocket": URLRouter(romanize.routing.websocket_urlpatterns),
        "http": get_asgi_application(),
    },
)
  • settings:
INSTALLED_APPS = [
    "channels",
    "romanize.apps.RomanizeConfig",
    "django.contrib.staticfiles",
    "rest_framework",
    "agencies.apps.AgenciesConfig",
]
WSGI_APPLICATION = "dwtools.wsgi.application"
ASGI_APPLICATION = "dwtools.routing.application"

I can pull up DRF’s standard views, create entries etc.

Then added a simple middleware that accesses the database (gets a count of the number of Agencies and puts it in request). Not even using MiddlewareMixin, just barebones init can call protocol.

System check identified no issues (0 silenced).
December 07, 2020 - 14:38:56
Django version 3.1.4, using settings 'dwtools.settings'
Starting ASGI/Channels version 3.0.2 development server at http://127.0.0.1:3401/
#             ^^^^^^^^               ^^^^^^^^^^^ ( Channels runserver)
Quit the server with CONTROL-C.
HTTP GET /agencies/ 200 [0.05, 127.0.0.1:60460]

The stack uses local nginx to route to port 3401 for /ws (websocket) and /agencies (http). Perhaps you spot something…

Leave a comment