3

Got an issue trying to connect from an async view in Django 3.1 to a sync view served by the same asgi server. Doing this in normal wsgi development server works, but not in an asgi server. Which seems kind of weird. Probably I misunderstood on how asgi works :). Here's a link on how to reproduce this:

All steps to reproduce this.

Here are just the views causing the problem. Maybe someone is able to tell immediately what I'm doing wrong by just looking at those:

import httpx

from django.http import JsonResponse


def sync_api_view(request):
    payload = {"foo": "bar"}
    return JsonResponse(payload)


def sync_aggregation_view(request):
    responses = []
    r = httpx.get("http://127.0.0.1:8000/sync_api_view/")
    responses.append(r.json())
    result = {"responses": responses}
    return JsonResponse(result)

The "sync_aggregation_view" is the one that works in wsgi but not via asgi.

ephes
  • 1,451
  • 1
  • 13
  • 19

1 Answers1

1

Ok, made a stupid mistake. Connecting to a single threaded server from within this server results in a deadlock. The reason why the wsgi django development server behaves differently is not wsgi or asgi, but because it's multithreaded by default since Django 1.4. I just didn't notice.

ephes
  • 1,451
  • 1
  • 13
  • 19