-
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
Multiple AsyncHTTPClient objects created for one asyncio event loop #3229
Comments
The AsyncHTTPClient class is unusual: it secretly reuses a single client object instead of creating a new one each time. This means that in general you're not supposed to close it. (Yes, this is very weird and I'd do it differently if I were starting over, but it's difficult to change at this point. See #2049) Most of the time, using
The most common case where you need to actually close the AsyncHTTPClient is in test cases, where you might want to run Or, if you want more isolation, you can use the
This would be a little cleaner if we supported the |
Thank you for the explanation and recommendations. Here are the details of what happened and why I wanted this feature, if you're interested: I have tried the first approach, where I simply define When I attempted to reuse the object passing it to my semaphored gather async fucntions from a central shared object defined only once, some calls would not execute and my application would stall, although this might be due to some unknowns in my code. |
That's not supposed to happen - that's the whole reason that we have all the magic to reuse a single instance. It should be limiting itself to Note that Windows is not a fully-supported platform for Tornado; scalability is limited in comparison to linux or macos. |
I have not reconfigured Here are the gather calls: Most frequent usage elsewhere: |
Your code looks fine to me; I'm not sure what might be going on. It could be a bug in Tornado. For example, we have an unresolved bug that can cause things to hang in some cases with a combination of redirects and timeouts (#3064). |
Thanks for checking it out. I have switched to aiohttp now. I noticed that while I was using Before/after: hclivess/nado@77fc179 I remember that when pressing CTRL-C, the application would spam me with "cannot schedule new futures after interpreter shutdown" for minutes so I assume that was coming from all the instances that were left in memory. |
Cool, I'm glad you found something that works for you. It does appear that you've found a bug that is allowing multiple AsyncHTTPClient objects to be created instead of reusing them (it's possible that the bug is in the asyncio-to-IOLoop mapping instead of in AsyncHTTPClient), so I'm going to retitle this issue and keep it open. FWIW, I see from your diff that you're using Tornado 6.1. There was a change in version 6.2 that might at least make this problem easier to diagnose: #3157. |
It appears that in some cases that the mechanism that reuses AsyncHTTPClient objects is failing, allowing multiple such objects to be created and, for example, exceed
max_clients
limitations. The problem could be in the asyncio-to-IOLoop instance cache or the IOLoop-to-AsyncHTTPClient mapping.Original message follows:
Greetings. I recently ran into a problem in my code where I have too many AsyncHTTPClient spawning. I attempted to resolve this problem by using a "with" statement so the object is automatically destroyed at the end. Unfortunately, such approach is not currently supported. Can you please add start and end so it can be used that way?
Minimal reproducible example:
My temporary solution:
The text was updated successfully, but these errors were encountered: