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
How to call tornado asynchronous function right?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
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
 
Li jiaming  
View profile  
 More options Oct 2 2012, 6:08 am
From: Li jiaming <li.jiam...@gmail.com>
Date: Tue, 2 Oct 2012 03:07:59 -0700 (PDT)
Local: Tues, Oct 2 2012 6:07 am
Subject: How to call tornado asynchronous function right?

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.
aliane abdelouahab  
View profile  
 More options Oct 2 2012, 5:51 pm
From: aliane abdelouahab <alabdeloua...@gmail.com>
Date: Tue, 2 Oct 2012 14:51:51 -0700 (PDT)
Local: Tues, Oct 2 2012 5:51 pm
Subject: Re: How to call tornado asynchronous function right?
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:


 
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.
A. Jesse Jiryu Davis  
View profile  
 More options Oct 2 2012, 9:33 pm
From: "A. Jesse Jiryu Davis" <ajesseda...@gmail.com>
Date: Tue, 2 Oct 2012 18:33:33 -0700 (PDT)
Local: Tues, Oct 2 2012 9:33 pm
Subject: Re: How to call tornado asynchronous function right?

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.


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


 
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.
Jimmy  
View profile  
 More options Oct 2 2012, 11:57 pm
From: Jimmy <li.jiam...@gmail.com>
Date: Tue, 2 Oct 2012 20:57:24 -0700 (PDT)
Local: Tues, Oct 2 2012 11:57 pm
Subject: Re: How to call tornado asynchronous function right?

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?


 
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.
Ben Darnell  
View profile  
 More options Oct 3 2012, 2:04 am
From: Ben Darnell <b...@bendarnell.com>
Date: Tue, 2 Oct 2012 23:04:34 -0700
Local: Wed, Oct 3 2012 2:04 am
Subject: Re: [tornado] Re: How to call tornado asynchronous function right?
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


 
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.
Lorenzo Bolla  
View profile  
 More options Oct 3 2012, 7:36 am
From: Lorenzo Bolla <lbo...@gmail.com>
Date: Wed, 3 Oct 2012 12:36:00 +0100
Subject: Re: [tornado] Re: How to call tornado asynchronous function right?

I figured that it might be useful to collect answers to all these
questions, that periodically are asked in this mailing list in a blog post.

Here it is:
http://lbolla.info/blog/2012/10/03/asynchronous-programming-with-torn...

If you have comments or you want me to add/correct something, just let me
know.

L.


 
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.
aliane abdelouahab  
View profile  
 More options Oct 3 2012, 10:03 am
From: aliane abdelouahab <alabdeloua...@gmail.com>
Date: Wed, 3 Oct 2012 07:03:39 -0700 (PDT)
Local: Wed, Oct 3 2012 10:03 am
Subject: Re: How to call tornado asynchronous function right?
thank you :D
like this, i'll learn the async :p

On 3 oct, 12:36, Lorenzo Bolla <lbo...@gmail.com> wrote:


 
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.
Jimmy  
View profile  
 More options Oct 3 2012, 10:41 pm
From: Jimmy <li.jiam...@gmail.com>
Date: Wed, 3 Oct 2012 19:41:42 -0700 (PDT)
Local: Wed, Oct 3 2012 10:41 pm
Subject: Re: [tornado] Re: How to call tornado asynchronous function right?

Thank you Lorenzo. That's really helpful!


 
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.
Jason Huang  
View profile  
 More options Oct 3 2012, 11:23 pm
From: Jason Huang <blu...@gmail.com>
Date: Wed, 3 Oct 2012 23:23:53 -0400
Local: Wed, Oct 3 2012 11:23 pm
Subject: Re: [tornado] Re: How to call tornado asynchronous function right?

Great, thanks for your summary!


 
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.
End of messages
« Back to Discussions « Newer topic     Older topic »