Note that there are some explanatory texts on larger screens.

plurals
  1. POWrapping a tornado.gen.engine-wrapped function with another function
    text
    copied!<p>Say, I have a function, that is wrapped with gen.engine to "straighten out" the callback chain, that is, so that the code appears to be synchronous / linear / whatever.</p> <p>The function, then looks like this</p> <pre><code>@gen.engine def func(): ... yield gen.Task(...) ... yield gen.Task(...) </code></pre> <p>and so on. I understand, that I definitely can use try / except around <code>yield</code>s to catch exceptions occuring in the function, that is wrapped by <code>gen.Task</code>. What if I need to wrap the function <code>func</code> itself in another function to (this is the actual use case) catch all "uncaught" exceptions in <code>func</code> without introducing an "ugly" (right..) try / except, that will span the entire <code>func</code>?</p> <p>I've come up with this:</p> <pre><code>@gen.engine def func(..., callback): ... callback() @gen.engine def outer(): try: yield gen.Task(func) except Exception as e: # Log the exception # Stop ioloop (or something) </code></pre> <p>This adds a bit of generality to <code>func</code>, but introduces an extra argument and some artificial logic in <code>func</code>.</p> <p>Is there any other way of doing this? Do note, that the "emergency exceptions catching" is more or less an artificial use case for the purpose of this question (this can probably done in some other way), what I am looking is the correct way of calling those tornado.gen.engine-wrapped functions from another functions.</p> <p>Edit: silly me, I should've mentioned that I am restricted to tornado 2.x!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload