Hey, these are three questions in one !; -)
Sometimes it can be blocked - even if your server generates data quite quickly, bottlenecks in the network can theoretically block your readings.
Reading URL data using "for dat in req" means reading a string at a time - not very useful if you are reading binary data such as an image. You get better control if you use
chunk = req.read(size)
which can, of course, be blocked.
Whether this will be the best way depends on the features not available in your question. For example, if you need to run without any blocking calls, you will need to consider a structure such as Twisted . If you donβt want to block to hold you and donβt want to use Twisted (which is a completely new paradigm compared to the blocking way of doing things), then you can expand the stream to read and write the file while your main stream continues have fun:
def func(req):
Obviously, I missed error checking / exception handling, etc., but hopefully this is enough to give you an image.
Vinay sajip
source share