Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion How to call tornado asynchronous function right?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jimmy  
View profile  
 More options Oct 2 2012, 11:52 pm
From: Jimmy <li.jiam...@gmail.com>
Date: Tue, 2 Oct 2012 20:52:52 -0700 (PDT)
Local: Tues, Oct 2 2012 11:52 pm
Subject: Re: How to call tornado asynchronous function right?

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:

> Do this:

> http://emptysquare.net/blog/pausing-with-tornado/

> 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:

>> look here, maybe it will help:

>> http://groups.google.com/group/python-tornado/browse_thread/thread/ae...

>> 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

>> > class MainHandler(tornado.web.RequestHandler):
>> >     def get(self):
>> >         print 'receive root request'
>> >         self.write("Hello, world")

>> > class SleepHandler(tornado.web.RequestHandler):
>> >     @asynchronous
>> >     @gen.engine
>> >     def get(self):
>> >         print 'receive sleep request'
>> >         yield [ gen.Task(self.sleep, 10),
>> >                 gen.Task(self.sleep, 9)]
>> >         self.write("Wake up")
>> >         self.finish()

>> >     def sleep(self, sec, callback):
>> >         print 'sleep for %d seconds' % sec
>> >         time.sleep(sec)
>> >         print 'finish sleep %d' % sec
>> >         return callback()

>> > application = tornado.web.Application([
>> >     (r"/", MainHandler),
>> >     (r"/sleep", SleepHandler),
>> > ])

>> > 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.

>> > class SleepHandler(tornado.web.RequestHandler):
>> >     @asynchronous
>> >     def get(self):
>> >         print 'receive sleep request'
>> >         self.async_callback(self.callback_sleep, 10)

>> >     def callback_sleep(self, sec):
>> >         print 'callback sleep for %d seconds' % sec
>> >         time.sleep(sec)
>> >         print 'finish callback sleep for %d seconds' % sec
>> >         self.write('wake up')
>> >         self.finish()


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.