I created a simple app with gen.task. But seems doesn't work asynchronously. I try to call /sleep for multiple times, but it handles request in sequence.
import tornado.ioloop import tornado.web from tornado.web import asynchronous from tornado import gen import time import tornado.httpserver
class MainHandler(tornado.web.RequestHandler): def get(self): print 'receive root request' self.write("Hello, world")
if __name__ == "__main__": server = tornado.httpserver.HTTPServer(application) server.listen(8888) tornado.ioloop.IOLoop.instance().start()
*Anything wrong in my code?*
I also tried async_callback (which is claimed to be obsoleted way of doing async). But it works. It can handle multiple requests in parallel when calling /sleep.
> I created a simple app with gen.task. But seems doesn't work
> asynchronously. I try to call /sleep for multiple times, but it handles
> request in sequence.
> import tornado.ioloop
> import tornado.web
> from tornado.web import asynchronous
> from tornado import gen
> import time
> import tornado.httpserver
> if __name__ == "__main__":
> server = tornado.httpserver.HTTPServer(application)
> server.listen(8888)
> tornado.ioloop.IOLoop.instance().start()
> *Anything wrong in my code?*
> I also tried async_callback (which is claimed to be obsoleted way of doing
> async). But it works. It can handle multiple requests in parallel when
> calling /sleep.
Tornado isn't magic. If you call sleep(), it'll pause your whole process. You must explicitly return control to the IOLoop to allow it to continue processing requests.
> On 2 oct, 11:08, Li jiaming <li.jiam...@gmail.com> wrote: > > I created a simple app with gen.task. But seems doesn't work > > asynchronously. I try to call /sleep for multiple times, but it handles > > request in sequence.
> > import tornado.ioloop > > import tornado.web > > from tornado.web import asynchronous > > from tornado import gen > > import time > > import tornado.httpserver
> > if __name__ == "__main__": > > server = tornado.httpserver.HTTPServer(application) > > server.listen(8888) > > tornado.ioloop.IOLoop.instance().start()
> > *Anything wrong in my code?*
> > I also tried async_callback (which is claimed to be obsoleted way of > doing > > async). But it works. It can handle multiple requests in parallel when > > calling /sleep.
I don't mean to put web app to sleep. I just use sleep to simulate some heavy load work. When one task is working, and new request for the same task comes in, tornado will execute those tasks in sequence, not in parallel. That is my question.
I use the old way (async_callback), which works. But the new way of yield gen.Task(???) does not work in parallel.
Anything wrong with my code using gen.Task?
On Wednesday, October 3, 2012 9:33:33 AM UTC+8, A. Jesse Jiryu Davis wrote:
> Tornado isn't magic. If you call sleep(), it'll pause your whole process. > You must explicitly return control to the IOLoop to allow it to continue > processing requests.
> On Tuesday, October 2, 2012 5:51:54 PM UTC-4, aliane abdelouahab wrote:
>> On 2 oct, 11:08, Li jiaming <li.jiam...@gmail.com> wrote: >> > I created a simple app with gen.task. But seems doesn't work >> > asynchronously. I try to call /sleep for multiple times, but it handles >> > request in sequence.
>> > import tornado.ioloop >> > import tornado.web >> > from tornado.web import asynchronous >> > from tornado import gen >> > import time >> > import tornado.httpserver
>> > if __name__ == "__main__": >> > server = tornado.httpserver.HTTPServer(application) >> > server.listen(8888) >> > tornado.ioloop.IOLoop.instance().start()
>> > *Anything wrong in my code?*
>> > I also tried async_callback (which is claimed to be obsoleted way of >> doing >> > async). But it works. It can handle multiple requests in parallel when >> > calling /sleep.
I don't mean to make it sleep. You can assume the sleep to some heavy load work, like loop i from 0 to 10000000000. When multiple requests come in concurrently, I saw the request is executed in sequence, not in parallel (using yield gen.Task). But the old way of async_callback works.
> On 2 oct, 11:08, Li jiaming <li.jiam...@gmail.com> wrote: > > I created a simple app with gen.task. But seems doesn't work > > asynchronously. I try to call /sleep for multiple times, but it handles > > request in sequence.
> > import tornado.ioloop > > import tornado.web > > from tornado.web import asynchronous > > from tornado import gen > > import time > > import tornado.httpserver
> > if __name__ == "__main__": > > server = tornado.httpserver.HTTPServer(application) > > server.listen(8888) > > tornado.ioloop.IOLoop.instance().start()
> > *Anything wrong in my code?*
> > I also tried async_callback (which is claimed to be obsoleted way of > doing > > async). But it works. It can handle multiple requests in parallel when > > calling /sleep.
Your async_callback version doesn't actually work - it never calls
callback_sleep. async_callback is meant to be used as a wrapper to
deal with exception handling - it was necessary in early versions of
Tornado, but not any more. You probably meant io_loop.add_callback
instead of async_callback, but if you make that change you'll see that
time.sleep blocks in that version as well.
@asynchronous is a declaration, not a directive: it describes the
fact that get() is asynchronous (i.e. there is more work to be done
after it returns), it doesn't make it so. Tornado's core is still a
single-threaded event loop, so to achieve concurrency within a single
process you need to use or make non-blocking versions of any
time-consuming functions you use (e.g. IOLoop.add_timeout instead of
time.sleep, or AsyncHTTPClient instead of HTTPClient or urllib), or
hand that work off to another thread or process.
On Tue, Oct 2, 2012 at 8:57 PM, Jimmy <li.jiam...@gmail.com> wrote:
> I don't mean to make it sleep. You can assume the sleep to some heavy load
> work, like loop i from 0 to 10000000000. When multiple requests come in
> concurrently, I saw the request is executed in sequence, not in parallel
> (using yield gen.Task). But the old way of async_callback works.
> How to use gen.Task to create async behavior?
> On Wednesday, October 3, 2012 5:51:54 AM UTC+8, aliane abdelouahab wrote:
>> On 2 oct, 11:08, Li jiaming <li.jiam...@gmail.com> wrote:
>> > I created a simple app with gen.task. But seems doesn't work
>> > asynchronously. I try to call /sleep for multiple times, but it handles
>> > request in sequence.
>> > import tornado.ioloop
>> > import tornado.web
>> > from tornado.web import asynchronous
>> > from tornado import gen
>> > import time
>> > import tornado.httpserver
>> > if __name__ == "__main__":
>> > server = tornado.httpserver.HTTPServer(application)
>> > server.listen(8888)
>> > tornado.ioloop.IOLoop.instance().start()
>> > *Anything wrong in my code?*
>> > I also tried async_callback (which is claimed to be obsoleted way of
>> > doing
>> > async). But it works. It can handle multiple requests in parallel when
>> > calling /sleep.
On Wed, Oct 3, 2012 at 7:04 AM, Ben Darnell <b...@bendarnell.com> wrote:
> Your async_callback version doesn't actually work - it never calls
> callback_sleep. async_callback is meant to be used as a wrapper to
> deal with exception handling - it was necessary in early versions of
> Tornado, but not any more. You probably meant io_loop.add_callback
> instead of async_callback, but if you make that change you'll see that
> time.sleep blocks in that version as well.
> @asynchronous is a declaration, not a directive: it describes the
> fact that get() is asynchronous (i.e. there is more work to be done
> after it returns), it doesn't make it so. Tornado's core is still a
> single-threaded event loop, so to achieve concurrency within a single
> process you need to use or make non-blocking versions of any
> time-consuming functions you use (e.g. IOLoop.add_timeout instead of
> time.sleep, or AsyncHTTPClient instead of HTTPClient or urllib), or
> hand that work off to another thread or process.
> -Ben
> On Tue, Oct 2, 2012 at 8:57 PM, Jimmy <li.jiam...@gmail.com> wrote:
> > I don't mean to make it sleep. You can assume the sleep to some heavy
> load
> > work, like loop i from 0 to 10000000000. When multiple requests come in
> > concurrently, I saw the request is executed in sequence, not in parallel
> > (using yield gen.Task). But the old way of async_callback works.
> > How to use gen.Task to create async behavior?
> > On Wednesday, October 3, 2012 5:51:54 AM UTC+8, aliane abdelouahab wrote:
> >> On 2 oct, 11:08, Li jiaming <li.jiam...@gmail.com> wrote:
> >> > I created a simple app with gen.task. But seems doesn't work
> >> > asynchronously. I try to call /sleep for multiple times, but it
> handles
> >> > request in sequence.
> >> > import tornado.ioloop
> >> > import tornado.web
> >> > from tornado.web import asynchronous
> >> > from tornado import gen
> >> > import time
> >> > import tornado.httpserver
> >> > I also tried async_callback (which is claimed to be obsoleted way of
> >> > doing
> >> > async). But it works. It can handle multiple requests in parallel when
> >> > calling /sleep.
> If you have comments or you want me to add/correct something, just let me
> know.
> L.
> On Wed, Oct 3, 2012 at 7:04 AM, Ben Darnell <b...@bendarnell.com> wrote:
> > Your async_callback version doesn't actually work - it never calls
> > callback_sleep. async_callback is meant to be used as a wrapper to
> > deal with exception handling - it was necessary in early versions of
> > Tornado, but not any more. You probably meant io_loop.add_callback
> > instead of async_callback, but if you make that change you'll see that
> > time.sleep blocks in that version as well.
> > @asynchronous is a declaration, not a directive: it describes the
> > fact that get() is asynchronous (i.e. there is more work to be done
> > after it returns), it doesn't make it so. Tornado's core is still a
> > single-threaded event loop, so to achieve concurrency within a single
> > process you need to use or make non-blocking versions of any
> > time-consuming functions you use (e.g. IOLoop.add_timeout instead of
> > time.sleep, or AsyncHTTPClient instead of HTTPClient or urllib), or
> > hand that work off to another thread or process.
> > -Ben
> > On Tue, Oct 2, 2012 at 8:57 PM, Jimmy <li.jiam...@gmail.com> wrote:
> > > I don't mean to make it sleep. You can assume the sleep to some heavy
> > load
> > > work, like loop i from 0 to 10000000000. When multiple requests come in
> > > concurrently, I saw the request is executed in sequence, not in parallel
> > > (using yield gen.Task). But the old way of async_callback works.
> > > How to use gen.Task to create async behavior?
> > > On Wednesday, October 3, 2012 5:51:54 AM UTC+8, aliane abdelouahab wrote:
> > >> On 2 oct, 11:08, Li jiaming <li.jiam...@gmail.com> wrote:
> > >> > I created a simple app with gen.task. But seems doesn't work
> > >> > asynchronously. I try to call /sleep for multiple times, but it
> > handles
> > >> > request in sequence.
> > >> > import tornado.ioloop
> > >> > import tornado.web
> > >> > from tornado.web import asynchronous
> > >> > from tornado import gen
> > >> > import time
> > >> > import tornado.httpserver
> > >> > I also tried async_callback (which is claimed to be obsoleted way of
> > >> > doing
> > >> > async). But it works. It can handle multiple requests in parallel when
> > >> > calling /sleep.
> If you have comments or you want me to add/correct something, just let me > know.
> L.
> On Wed, Oct 3, 2012 at 7:04 AM, Ben Darnell <b...@bendarnell.com<javascript:> > > wrote:
>> Your async_callback version doesn't actually work - it never calls >> callback_sleep. async_callback is meant to be used as a wrapper to >> deal with exception handling - it was necessary in early versions of >> Tornado, but not any more. You probably meant io_loop.add_callback >> instead of async_callback, but if you make that change you'll see that >> time.sleep blocks in that version as well.
>> @asynchronous is a declaration, not a directive: it describes the >> fact that get() is asynchronous (i.e. there is more work to be done >> after it returns), it doesn't make it so. Tornado's core is still a >> single-threaded event loop, so to achieve concurrency within a single >> process you need to use or make non-blocking versions of any >> time-consuming functions you use (e.g. IOLoop.add_timeout instead of >> time.sleep, or AsyncHTTPClient instead of HTTPClient or urllib), or >> hand that work off to another thread or process.
>> -Ben
>> On Tue, Oct 2, 2012 at 8:57 PM, Jimmy <li.ji...@gmail.com <javascript:>> >> wrote: >> > I don't mean to make it sleep. You can assume the sleep to some heavy >> load >> > work, like loop i from 0 to 10000000000. When multiple requests come in >> > concurrently, I saw the request is executed in sequence, not in parallel >> > (using yield gen.Task). But the old way of async_callback works.
>> > How to use gen.Task to create async behavior?
>> > On Wednesday, October 3, 2012 5:51:54 AM UTC+8, aliane abdelouahab >> wrote:
>> >> On 2 oct, 11:08, Li jiaming <li.jiam...@gmail.com> wrote: >> >> > I created a simple app with gen.task. But seems doesn't work >> >> > asynchronously. I try to call /sleep for multiple times, but it >> handles >> >> > request in sequence.
>> >> > import tornado.ioloop >> >> > import tornado.web >> >> > from tornado.web import asynchronous >> >> > from tornado import gen >> >> > import time >> >> > import tornado.httpserver
>> >> > I also tried async_callback (which is claimed to be obsoleted way of >> >> > doing >> >> > async). But it works. It can handle multiple requests in parallel >> when >> >> > calling /sleep.
>> If you have comments or you want me to add/correct something, just let me
>> know.
>> L.
>> On Wed, Oct 3, 2012 at 7:04 AM, Ben Darnell <b...@bendarnell.com> wrote:
>>> Your async_callback version doesn't actually work - it never calls
>>> callback_sleep. async_callback is meant to be used as a wrapper to
>>> deal with exception handling - it was necessary in early versions of
>>> Tornado, but not any more. You probably meant io_loop.add_callback
>>> instead of async_callback, but if you make that change you'll see that
>>> time.sleep blocks in that version as well.
>>> @asynchronous is a declaration, not a directive: it describes the
>>> fact that get() is asynchronous (i.e. there is more work to be done
>>> after it returns), it doesn't make it so. Tornado's core is still a
>>> single-threaded event loop, so to achieve concurrency within a single
>>> process you need to use or make non-blocking versions of any
>>> time-consuming functions you use (e.g. IOLoop.add_timeout instead of
>>> time.sleep, or AsyncHTTPClient instead of HTTPClient or urllib), or
>>> hand that work off to another thread or process.
>>> -Ben
>>> On Tue, Oct 2, 2012 at 8:57 PM, Jimmy <li.ji...@gmail.com> wrote:
>>> > I don't mean to make it sleep. You can assume the sleep to some heavy
>>> load
>>> > work, like loop i from 0 to 10000000000. When multiple requests come in
>>> > concurrently, I saw the request is executed in sequence, not in
>>> parallel
>>> > (using yield gen.Task). But the old way of async_callback works.
>>> > How to use gen.Task to create async behavior?
>>> > On Wednesday, October 3, 2012 5:51:54 AM UTC+8, aliane abdelouahab
>>> wrote:
>>> >> On 2 oct, 11:08, Li jiaming <li.jiam...@gmail.com> wrote:
>>> >> > I created a simple app with gen.task. But seems doesn't work
>>> >> > asynchronously. I try to call /sleep for multiple times, but it
>>> handles
>>> >> > request in sequence.
>>> >> > import tornado.ioloop
>>> >> > import tornado.web
>>> >> > from tornado.web import asynchronous
>>> >> > from tornado import gen
>>> >> > import time
>>> >> > import tornado.httpserver
>>> >> > I also tried async_callback (which is claimed to be obsoleted way of
>>> >> > doing
>>> >> > async). But it works. It can handle multiple requests in parallel
>>> when
>>> >> > calling /sleep.