-
-
Notifications
You must be signed in to change notification settings - Fork 598
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
UserWarning: Unclosed httpx.AsyncClient #1224
Comments
|
what would call that method @AndrewMagerman |
you'd call it at the very end of your program i.e.
|
@bitsofinfo I had the same problem. The solution is that if you are using the EDIT -- this implementation closes the client prematurely, see the latest response for a better implementation class Provider:
def __init__(self):
self.client = zeep.AsyncClient(wsdl=wsdl_path, **zeep_client_kwargs)
async def do_something(self):
async with self.client as client:
response = client.service.someSoapMethod(
..
)
serialized_response = zeep.helpers.serialize_object(response) My problem was that I was using it like this, incorrectly: class Provider:
def __init__(self):
self.client = zeep.AsyncClient(wsdl=wsdl_path, **zeep_client_kwargs)
async def do_something(self):
response = self.client.service.someSoapMethod(
..
)
serialized_response = zeep.helpers.serialize_object(response) The python-zeep/src/zeep/client.py Lines 237 to 241 in c7d916c
And the python-zeep/src/zeep/transports.py Lines 205 to 206 in c7d916c
|
My suggestion above with the context manager has a problem where the httpx client is closed and it cannot be reused, thus eliminating the advantage of using a The suggestion @AndrewMagerman had is a good one but this is what I did to manage the lifecycle in the class Provider:
def __init__(self):
self.client = zeep.AsyncClient(wsdl=wsdl_path, **zeep_client_kwargs)
async def do_something(self):
response = self.client.service.someSoapMethod(
..
)
serialized_response = zeep.helpers.serialize_object(response)
...
def __del__(self):
"""
Ensures that the client is closed on exit. More information at https://github.com/mvantellingen/python-zeep/issues/1224#issuecomment-1000442879
"""
loop = asyncio.get_event_loop()
if loop.is_running():
loop.create_task(self._close_client())
else:
loop.run_until_complete(self._close_client())
async def _close_client(self):
await self.client.__aexit__() I can't take all the credit from this, most of this comes from https://stackoverflow.com/a/67577364/629263. More information on the non-context-managed version of httpx AsyncClient at encode/httpx#769 (comment) |
When using
zeep.AsyncClient()
how should it be cleaned up on exit?When the program exits:
The text was updated successfully, but these errors were encountered: