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
Asynchronous exception handling
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
  6 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
 
Serge S. Koval  
View profile  
 More options Oct 5 2012, 11:56 am
From: "Serge S. Koval" <serge.ko...@gmail.com>
Date: Fri, 5 Oct 2012 18:56:18 +0300
Local: Fri, Oct 5 2012 11:56 am
Subject: Asynchronous exception handling

Hi,

 I have following problem in regards to exception handling:
1. I have piece of code which polls some data source, say every second
2. If there's some data, it will process it and it might take, say, a minute
3. While processing data, it might crash. In this case it should retry from
the beginning second later.

Here's gist to illustrate approach: https://gist.github.com/3840620

Even though there's only 'raise' inside of 'with' statement, lets pretend
that this is some code that fails and it is asynchronous: using gen.Task to
make some asynchronous calls - you can't use try/catch.

This code contains one big problem: it leaks ExceptionStackContext every
time exception handler re-queues next iteration. So on next iteration, _exc
will be called twice: once for outer ExceptionStackContext, once for inner
ExceptionStackContext. Next time, will be called 3 times, etc.

I can't suppress exceptions either - if my exception handler returns True,
I will see '.. success' printed but it was not successful operation.

I have feeling that wrapping with NullStackContext won't help either - it
will keep growing stack anyway.

What is the best way to accomplish this task?

Thank you,
Serge.


 
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.
Serge S. Koval  
View profile  
 More options Oct 5 2012, 12:20 pm
From: "Serge S. Koval" <serge.ko...@gmail.com>
Date: Fri, 5 Oct 2012 19:20:07 +0300
Local: Fri, Oct 5 2012 12:20 pm
Subject: Re: Asynchronous exception handling

As a small update - looks like I need to read documentation more often.

Since python 2.5, you can mix try/finally with generators, so you don't
have to use ExceptionStackContext with tornado.gen to handle exceptions.

However, if you need to handle exceptions with callbacks, you need to wrap
code that reschedules next run with NullContext to prevent stack context
overflow and multiple calls to error handler.

Serge.

On Fri, Oct 5, 2012 at 6:56 PM, Serge S. Koval <serge.ko...@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 5 2012, 1:03 pm
From: "A. Jesse Jiryu Davis" <ajesseda...@gmail.com>
Date: Fri, 5 Oct 2012 10:03:46 -0700 (PDT)
Local: Fri, Oct 5 2012 1:03 pm
Subject: Re: Asynchronous exception handling

Could you post your fixed code for our edification? I'm still learning
about StackContexts.


 
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.
Serge S. Koval  
View profile  
 More options Oct 5 2012, 1:59 pm
From: "Serge S. Koval" <serge.ko...@gmail.com>
Date: Fri, 5 Oct 2012 20:58:53 +0300
Local: Fri, Oct 5 2012 1:58 pm
Subject: Re: [tornado] Re: Asynchronous exception handling

Sure, here's gist which illustrates exception handling with
ExceptionStackContext and try/catch: https://gist.github.com/3841339

Serge.

On Fri, Oct 5, 2012 at 8:03 PM, A. Jesse Jiryu Davis
<ajesseda...@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 5 2012, 5:31 pm
From: "A. Jesse Jiryu Davis" <ajesseda...@gmail.com>
Date: Fri, 5 Oct 2012 14:31:41 -0700 (PDT)
Local: Fri, Oct 5 2012 5:31 pm
Subject: Re: [tornado] Re: Asynchronous exception handling

OK, I think I mostly understand now. So Motor is working correctly with
StackContexts, you think?

Your second example, with a simple try / finally, is definitely the way I'd
intended people to handle exceptions from Motor.


 
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 6 2012, 12:12 am
From: Ben Darnell <b...@bendarnell.com>
Date: Fri, 5 Oct 2012 21:12:05 -0700
Local: Sat, Oct 6 2012 12:12 am
Subject: Re: [tornado] Re: Asynchronous exception handling
Instead of NullContext, you might try structuring things so that the
scheduler is in a separate StackContext "layer" than the worker
function (like ioloop.PeriodicCallback).  Note that PeriodicCallback
will mostly work fine as-is with async functions, but for overrun
protection you need something that knows its function is asynchronous.

class PeriodicAsyncCallback(PeriodicCallback):
  def _run(self):
    if not self._running:
      return
    with ExceptionStackContext(functools.partial(self.handle_exc,
self.counter)):
      self.callback(callback=functools.partial(self.handle_finished,
self.counter))

  def handle_exc(self, counter, typ, value, tb):
    logging.error("error in PeriodicAsyncCallback", exc_info=(typ,value,tb))
    if self.counter == counter:
      self._schedule_next()

  def handle_finished(self, counter):
    if self.counter == counter:
      self._schedule_next()

  def _schedule_next(self)
    # counter ensures that even if a callback generates multiple
exceptions in its
    # stack context we won't get multiple copies rescheduled.
    self.counter += 1
    super(PeriodicCallback, self)._schedule_next()

On Fri, Oct 5, 2012 at 9:20 AM, Serge S. Koval <serge.ko...@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.
End of messages
« Back to Discussions « Newer topic     Older topic »