The server:
from tornado import ioloop, web, gen
class MainHandler(web.RequestHandler):
@gen.coroutine
def get(self):
self.write('1' * 1024)
self.flush(callback=(yield gen.Callback('flush')))
yield gen.Wait('flush')
print 'First chunk sent'
self.write('2' * 1024)
self.flush(callback=(yield gen.Callback('flush')))
yield gen.Wait('flush')
print 'Second chunk sent'
self.finish()
application = web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8000)
ioloop.IOLoop.instance().start()
And the client:
import urllib2
urllib2.urlopen('http://127.0.0.1:8000/').close()
After request I see in the server console next:
First chunk sent
Second chunk sent
But I hope to see nothing there. What don't I understand?
You hope to block on flush() until the client has read that number of bytes?
flush() only ensures that you've written the bytes to the OS's network output buffer. The buffer is likely much larger than 3k. On my Linux box it's about 200k:
$ cat /proc/sys/net/core/wmem_default
212992
There's no way to block until the client has read your message. If you need to wait until the client has received the message, the client should send a confirmation message and you should listen for it.
The problem was with urllib2. It works prefect with next socket-baset client:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 8000))
s.send("GET / HTTP/1.0\r\n\r\n")
s.close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.