-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Any particular reason why calls to _handle_request_exception(e) or on_finish() are not being awaited ? #3038
Comments
In the implementation there's no reason for that method to be async. By convention methods that are prepended with an underscore are private and generally shouldn't be overridden by subclasses. You're probably better off doing something like class MyRequestHandler(RequestHandler):
async def get(self):
try:
...
except Exception as e:
await my_async_exception_handler(e) |
OK, _handle_request_exception(e) is private I get that, but on_finish() is public. Regarding _handle_request_exception, I have to deal with some old code which I've inherited from folks who are no longer with the company. And they decided to overwrite and use this method to handle errors that are not being caught at the controllers level, as a last line of defense. So I'm kind of stuck with this approach as it is a big project and I'm not planning to change everything to fix this. Anyhow I will see what I can do. |
You're right about |
You can use |
For all three methods ( |
My problem is that I'm trying to migrate sqlalchemy to use the new async support (this is a new feature. Couple of month old). And I have some calls to sqlalchemy in my overwritten _handle_request_exception and on_finish calls too. The problem with the sqlalchemy is that once I switch to async calls, I cannot run sync anymore. On top of this I am also cleaning up some context_vars on these 2 methods. So I can manage to trigger the async execution of the sqlalchemy calls, from the sync methods, using a task, but I'm having problems cleaning up the context vars. Maybe you can give me some ideas on how I can achieve this?
My code looks something like this. I was also trying to wait for the task completion but I believe because time.sleep is blocking, the task never gets completed. `def _handle_request_exception(self, e):
Thanks |
Can you please let me know which are the chances to get async support for on_finish and an async hook to handle request exceptions ? I won't be able to complete my conversion without these changes, so at least I can decide if I should continue or not. |
It's doable, but I have very little time for Tornado these days and adapting to the changes in Python 3.10 takes priority, so it might be a while before I get to it. There's a design question around making these methods async: What, if anything, happens when the async parts complete? Do we block starting the next request on the connection while the async To make |
log_request method should also offer async support. |
I also need support for an async |
Async for on_finish would be really useful. I am also migrating to async sqlalchemy and need to be able close the session with async. I've seen |
Actually async on_finish would not help my particular issue (although it would still be nice to have). I simulated it by overriding the _execute method and still had the same issue where DB sessions don't always clean up on time. I think it's because the test env and server share the same event loop, so as soon as the Found |
Yes, the design challenge here is that |
Agreed, finishing async after the request has finished is all around better for prod. I did find it quite frustrating to get DB teardown to work reliably in tests though because of that. Even with close_all_sessions there were still random failures due to connections left open in the connection pool, despite closing all sessions and disposing the engine. Ended up force-dropping the db in my test suite in order to get it working 100% of the time. Anyway, not a tornado problem at this point. Thanks for the insight! |
Hi,
Any particular reason why methods calls to _handle_request_exception(e) or on_finish() are not being awaited ?
tornado/tornado/web.py
Line 1709 in 1db5b45
In my implementation I'm overwriting these methods, but I need async support on them. I was trying to use asyncio functionality to handle my async calls , but it doesn't work properly in my project.
It's much easier to make a sync call from an async method then to make an async call from a sync method.
Thanks
The text was updated successfully, but these errors were encountered: