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
why ExceptionStackContext can't throw exception out of ioloop?
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
  2 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
 
Jimmy  
View profile  
 More options Nov 6 2012, 9:30 pm
From: Jimmy <li.jiam...@gmail.com>
Date: Tue, 6 Nov 2012 18:30:12 -0800 (PST)
Local: Tues, Nov 6 2012 9:30 pm
Subject: why ExceptionStackContext can't throw exception out of ioloop?

I'm trying to write a decorator to make async call to be called in an
synchronous way (because the async function will be shared by tornado async
caller, and script sync caller). But the behavior seems different for
exceptions in callback or not in callback.

Here is my sample code (inspired by bergundy and A. Jesse in this
thread: https://groups.google.com/forum/?fromgroups=#!topic/python-tornado/lH...
)

from tornado import ioloop, gen
from tornado.stack_context import ExceptionStackContext
def sync(fn):
    def wrapper(*args, **kwargs):
        ret = dict(val=None)
        def stop(*args):
            ret['val'] = args
            ioloop.IOLoop.instance().stop()

        def exception_callback(*args, **kwargs):
            print 'in exception callback'
            ioloop.IOLoop.instance().stop()
            print args
            raise args[1]

        with ExceptionStackContext(exception_callback):
            kwargs['callback'] = stop
            fn(*args, **kwargs)
            ioInstance = ioloop.IOLoop.instance()
            ioInstance.start()
        return ret['val']

    return wrapper

class HTTPCaller(object):
    @gen.engine
    def call(self, callback):
        http_client = httpclient.AsyncHTTPClient()
        response = yield gen.Task(http_client.fetch,
"https://www.googleaaaa.com/")  ## I made this wrong url in purpose.
        if response.error:
            print 'rethrow exception'
            response.rethrow()
        else:
            callback(response.body)

if __name__ == "__main__":

    print 'calling sync test'
    try:
        print 'get sync response = '+str(sync(HTTPCaller().call)())
    except Exception as ex:
        print 'catch exception %s' % (str(ex))
    print 'complete sync test'

I use wrong url to trigger exceptions. If I disconnect network, the
exception will be captured in the main function ("nodename nor servname
provided, or not known"). If I connect network, the exception is captured
by ExceptionStackContext, but not thrown to main function (HTTP 599:
Timeout), the return value in main function is None.

I'm wondering, what makes the difference? How can I throw the exception to
the main function for the timeout scenario?

Thank you in advance.


 
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 Nov 8 2012, 9:54 am
From: Ben Darnell <b...@bendarnell.com>
Date: Thu, 8 Nov 2012 06:54:08 -0800
Local: Thurs, Nov 8 2012 9:54 am
Subject: Re: [tornado] why ExceptionStackContext can't throw exception out of ioloop?

After you call IOLoop.stop() you're still in the callback context.  What
you want to do is save the exception in exception_callback, and re-raise it
outside the IOLoop, i.e. after the 'with ExceptionStackContext' block.  See
AsyncTestCase.wait() for an example of this.

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