I wrote the following code:
@tornado.web.stream_request_bodyclass MyHandler(tornado.web.RequestHandler): @coroutine def prepare(self):
self.client = tornado.httpclient.AsyncHTTPClient() self.req = tornado.httpclient.HTTPRequest( url, method, body_producer=self.body_producer, headers ) self.write_func = None self.producer_future = Future() try: response = yield self.client.fetch(self.req) print(response) except Exception as ex: print ('Exception:', ex)
def body_producer(self, write):
if self.write_func is None:
self.write_func = write
return self.producer_future
@coroutine
def data_received(self, chunk):
if self.write_func is not None:
yield self.write_func(chunk)
@coroutine def put(self): self.producer_future.set_result(None) self.finish()--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
"""Demo of streaming requests with Tornado.This script features a client using AsyncHTTPClient's body_producerfeature to slowly produce a large request body, and two serverhandlers to receive this body (one is a proxy that forwards to theother, also using body_producer).It also demonstrates flow control: if --client_delay is smaller than--server_delay, the client will eventually be suspended to allow theserver to catch up. You can see this in the logs, as the "clientwriting" lines are initially once a second but eventually become lessfrequent (the details are platform-dependent and adjustable withsetsockopt, but with the defaults on my Mac the delays start to showup around chunk 18).Tested with Python 3.4, Tornado 4.1.dev1 (master branch from 11 Jan 2015),and Toro 0.7.Runs on Tornado 4.0 and higher, but 4.0 has a bug with flow control onkqueue platforms (Mac and BSD) so to see the flow control effects onthose platforms you'll need latest source from github (until 4.1 is released)."""
Ben, it works! Thanks a lot!But i don't understand, how body_producer is called before "yield self.fetch_future"?