-
Notifications
You must be signed in to change notification settings - Fork 15
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
Add nwis_client iv context handler #257
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the addition, @JoshCu! This issue looks similar to #237 (comment) and requests-cache/aiohttp-client-cache#173. I want to investigate the root cause of this, but I don't think that should hold up your addition. Could you add a "slow" unit test that creates a IVDataService
instance and uses the context manager? Use test_nwis_client_cache_path
for inspiration. You should more or less be able to copy and paste the test and just use the context manager instead.
Thanks again for contributing!
Thanks for the links! Is asserting that the _restclient._session is closed sufficient? I tried an assertion that the event loop was closed too but it fails as the _loop is still open. Unless it's instantiating a new one by accessing the property? |
@aaraney Did you want to merge then update or update then merge? |
Sorry, i'm just now getting back to this. I don't think the behavior you found @JoshCu is avoidable on our end (more on that in a bit). The easiest way to get around this is by wrapping your code in a function. For example: #!/usr/bin/env python
from hydrotools.nwis_client import IVDataService
def main():
service = IVDataService()
print("getting data")
usgs_data = service.get(sites="10154200", startDT="2001-01-01", endDT="2001-01-02")
print("done, exiting")
return usgs_data
if __name__ == "__main__":
df = main() The above will not hang. Now as to why I think this is happening. Ive run into weird reference counting issues with python's Without getting too much into the weeds, I think this is happening because the We can manually "drop" our reference to from hydrotools.nwis_client import IVDataService
service = IVDataService()
print("getting data")
usgs_data = service.get(sites="10154200", startDT="2001-01-01", endDT="2001-01-02")
# explicitly drop ref to IVDataService; this decrements the ref count to 0 and schedules `__del__` which calls `close()`
service = None
print("done, exiting") |
Thanks for adding this, @JoshCu! 🎉 |
What a strange edge case, thanks for the explanation! When I first encountered it I was testing a super basic functionless script. This also explains why I was having such a hard time reproducing the issue within the tests. When I was looking at it over the weekend, I thought I fixed it at least 5 times only for it not to work when I tested it outside of pytest. I've had multiprocessing module and flask also complain when there's no main function. Maybe the real solution is for me to not be lazy and actually use functions. |
@JoshCu, no kidding 😅! For sure! Kudos for trying to reproduce it in
Likewise, don't get me started on multiprocessing haha. So many platform and version specific edge cases.
Hey, but if you would have done that, you never would have found and fixed this issue! |
When using the IVDataService class to get usgs data, the rest client threads keep running after the main program has finished executing.
I managed to "fix" it by adding a context handler, but I'm not familiar enough with asyncio to make it work without the
with
syntax.Test
Fix
Note
To clarify: This does not entirely fix the original issue, the threads will not close unless you use the with context handler. The "test" code will cause the bug with and without this change.