In django asynchronous view, multiple consecutive requests will not run?
Why do some requests run the test_async() function, but some do not run. Is this because Django’s asynchronous view is unstable?Maybe my writing is wrong. It seems that I have overlooked something, which master can help me?
I wrote a simple asynchronous view, but I don’t know if it’s completely correct.
from django.http import HttpResponse
from asgiref.sync import sync_to_async
import asyncio
import time
@sync_to_async
def test_async():
print('test_async start')
time.sleep(5)
print('test_async end')
#view function
async def wechatAPI(request):
asyncio.create_task(test_async())
return HttpResponse('success')
I manually request through the browser, and the click frequency is not fast. But the response to this request seems too late to be processed, and it is not exactly the case.
Why can’t it respond to every request like a message queue, no matter how late? Maybe it’s because Django asynchronous view can’t do the function like message queue at all?
test_async start
[22/Jun/2021 11:25:05] “GET /wechat/ HTTP/1.1” 200 7
[22/Jun/2021 11:25:07] “GET /wechat/ HTTP/1.1” 200 7
[22/Jun/2021 11:25:07] “GET /wechat/ HTTP/1.1” 200 7
test_async end
[22/Jun/2021 11:25:10] “GET /wechat/ HTTP/1.1” 200 7
[22/Jun/2021 11:25:11] “GET /wechat/ HTTP/1.1” 200 7
[22/Jun/2021 11:25:12] “GET /wechat/ HTTP/1.1” 200 7
[22/Jun/2021 11:25:12] “GET /wechat/ HTTP/1.1” 200 7
[22/Jun/2021 11:25:13] “GET /wechat/ HTTP/1.1” 200 7
[22/Jun/2021 11:25:14] “GET /wechat/ HTTP/1.1” 200 7
[22/Jun/2021 11:25:18] “GET /wechat/ HTTP/1.1” 200 7
[22/Jun/2021 11:25:20] “GET /wechat/ HTTP/1.1” 200 7
[22/Jun/2021 11:25:21] “GET /wechat/ HTTP/1.1” 200 7
[22/Jun/2021 11:25:22] “GET /wechat/ HTTP/1.1” 200 7
[22/Jun/2021 11:25:23] “GET /wechat/ HTTP/1.1” 200 7
[22/Jun/2021 11:25:24] “GET /wechat/ HTTP/1.1” 200 7
test_async start
[22/Jun/2021 11:25:25] “GET /wechat/ HTTP/1.1” 200 7
[22/Jun/2021 11:25:26] “GET /wechat/ HTTP/1.1” 200 7
[22/Jun/2021 11:25:27] “GET /wechat/ HTTP/1.1” 200 7
test_async end