I am writing a webservice in Django to handle image/video streams, but it's mostly done in an external program. For instance:
- client requests for
/1.jpg?size=300x200
- python code parse
300x200
in django (or other WSGI app) - python calls
convert
(part of Imagemagick) usingsubprocess
module, with parameter 300x200 convert
reads 1.jpg from local disk, convert to size accordingly- Writing to a temp file
- Django builds
HttpResponse()
and read the whole temp file content as body
As you can see, the whole temp file read-then-write process is inefficient. I need a generic way to handle similar external programs like this, not only convert
, but others as well like cjpeg
, ffmepg
, etc. or even proprietary binaries.
I want to implement it in this way:
- python gets the stdout
fd
of theconvert
child process - chain it to WSGI socket fd for output
I've done my homework, Google says this kind of zero-copy could be done with system call splice()
. but it's not available in Python. So how to maximize performance in Python for these kind of scenario?
- Call splice() using
ctypes
? - hack memoryview() or buffer() ?
- subprocess has
stdout
which hasreadinto()
, could this be utilized somehow? - How could we get fd number for any WSGI app?
I am kinda newbie to these, any suggestion is appreciated, thanks!