Currently, ASGI applications must read and send each file chunk in Python to transmit file data. This is inefficient since it steals a lot of GIL time for something that is an IO-bound task.
Pure-python ASGI webservers could implement pathsend as something akin to asyncio.to_thread( os.sendfile, ... ). When wrapping the operating system's syscall in a thread, it would would free up a ton of GIL time, especially for multiple concurrent huge payloads.
If os.sendfile doesn't exist (such as on Windows) then it would need to fall-back to traditional python chunking.
Based on current implementations, the determination of whether to use os.sendfile will need to be left up to the event loop's loop.sendfile implementation.
I can PR this behavior, just let me know if you agree.
Ref:
Currently, ASGI applications must read and send each file chunk in Python to transmit file data. This is inefficient since it steals a lot of GIL time for something that is an IO-bound task.
Pure-python ASGI webservers could implement
pathsendas something akin toasyncio.to_thread( os.sendfile, ... ). When wrapping the operating system's syscall in a thread, it would would free up a ton of GIL time, especially for multiple concurrent huge payloads.If
os.sendfiledoesn't exist (such as on Windows) then it would need to fall-back to traditional python chunking.Based on current implementations, the determination of whether to use
os.sendfilewill need to be left up to the event loop'sloop.sendfileimplementation.I can PR this behavior, just let me know if you agree.
Ref:
offsetandcountto Path Send django/asgiref#469