1

I've set up a trial async view in my Django app but the view continues to render in sync. As per Django docs, I'm checking that my Middleware isn't causing the issue:

Middleware can be built to support both sync and async contexts. Some of Django’s middleware is built like this, but not all. To see what middleware Django has to adapt, you can turn on debug logging for the django.request logger and look for log messages about “Synchronous middleware … adapted”.

There's already been a Stack Overflow question to elaborate on using the logger to find which middleware is prevent async from working, but the answer is incomplete.

This is what I have in my settings, as per the above Stack Overflow answer:

settings.py

LOGGING = {  
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['console'],
            'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG'),
            'propagate': False,
        },
    },
}

And my view:

views.py

from time import sleep
import asyncio
import logging
logger = logging.getLogger("info")

# --- Views
from django.db import transaction 
@transaction.non_atomic_requests
async def async0(request):
    #loop = asyncio.get_event_loop()
    #loop.create_task(lets_sleep())
    await asyncio.sleep(1)
    logger.debug('in index')
    logger.info('something')
    return HttpResponse("Hello, async Django!")

I've restarted the built in Django server, but I don't see a log output anywhere. Where should I be looking ?

rcx935
  • 217
  • 5
  • 15

1 Answers1

0

although you posted your view, the log is about the middleware, so you should have a sync only middleware to get the log you want. after this, you need to add root logger as well:

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
        },
    },
    "root": {
        "level": "DEBUG",
    },
    "loggers": {
        "django.request": {
            "handlers": ["console"],
            "level": "DEBUG",
        },
    },
}

this was enough for me to get the Synchronous middleware someMiddlewareOfYours adapted log msg

rado
  • 5,720
  • 5
  • 29
  • 51