cannot request get/post to external api with asgi mode

django: 3.26
daphne: 3.0.2
requests: 2.26.0

in my views:

def get_home(request):
  session = requests.session()
  resp = session.get('http://' + settings.API_ADDR_URL, headers=headers, auth=settings.API_AUTH)
  if resp.status_code ==
    # do something

And the settings.API_ADDR_URL is another app in the same django project

when i start the django with:
$ python runserver --noasgi
this view can be displayed normally

when i start the django with:
$ python runserver
the django app will be hangup, and cannot handle any request from browser

and exclude two scenarios:

  1. other code caused: the django app can handle any view without requests invoking
  2. proxy: ‘wget’ can get response before request this view, after is hangup too.

who can help me, i have some mistakes with daphne? or it is one bug?

To clarify, are you saying that when you run it this way, your Django app can’t handle any requests, even for different URLs? Or is it only this URL that is failing? (And when it fails, then prevents any other URLs from being handled.)

Can you confirm that the other app receives and has responded to the request that’s started in get_home?

Cannot handle any requests, include different URLs, after requesting this view.
Before requesting this view, other URLs can respond normally

The other app cannot respond any request, this start after i request to get_home, before request get_home(), others URLs can respond normally

(Emphasis added.)

I missed this exceedingly important point here.

Yes. this is going to hang everything up. The standard requests module is not async friendly. So, it’s going to issue the get and then block the processing thread. Since the processing thread is stopped, the request to that API is never going to execute. In fact, everything is going to stop, which is why no other URLs respond either.

See the Blocking or Non-Blocking section of the requests docs.

You’ve got at least three choices that come to mind:

  1. Call your API directly through a function call rather than trying to do it through an http request. (It may require an interface function to allow that to work.)

  2. Use the sync_to_async function to have get_home run in a separate thread.

  3. Use one of the async-friendly wrappers around requests.