5 ms·
> Or just run 20 workers and let the OS handle async. OSes are pretty good at this. Async is pretty hard to reason about. def slow_sync_view(request):
by maxmalysh 4y ago
> Or just run 20 workers and let the OS handle async. OSes are pretty good at this. Async is pretty hard to reason about.
def slow_sync_view(request):
# these requests won't be executed in parallel;
# async version could eliminate this extra latency
foo = requests.get('https://www.google.com/humans.txt').text
bar = requests.get('https://checkip.amazonaws.com').text
return HttpResponse(f'{foo}\n{bar}')
- pdhborges 4y agoI think you are being down voted because a straight port of this code to async would still run one request after the other. You would have to queue one task for each request in the event loop and then await for them both to gain some parallelism in the I/O section of the code.
- KptMarchewa 4y agoBasic concurrent.futures.ThreadPoolExecutor solves this problem. https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.Executor https://docs.python.org/3/library/concurrent.futures.html#co...