Skip to content

asynclient not closing with aclose : UserWarning: Unclosed <httpx.AsyncClient object at 0x000001EEBB46E520>. #1813

@qwerty32123

Description

@qwerty32123

Client.py

import json
import ssl
import collections
import traceback
import httpx



class http_Client():
    def __init__(self):
        self.proxy = None
        self.headers = None
        self.body = None
        self.params = None
        self.url = None
        self.method = None
        self.cookies = None
        self.response = None
        self.remove = None

    def reset(self):
        self.headers = None
        self.body = None
        self.params = None
        self.url = None
        self.method = None
        self.cookies = None
        self.response = None
        self.remove = None


    async def run(self):

        ssl_config = httpx._config.SSLConfig()

        ssl_context = ssl_config.load_ssl_context()

        ssl_context.options |= getattr(ssl, "OP_NO_TLSv1_3", 0)

        ssl_context.load_verify_locations(path)

        #ssl_config = client_httpx.SSLConfig(verify=path')

        try:
            self.headers = collections.OrderedDict(self.headers)
            if self.proxy != None:
                client = httpx.AsyncClient(proxies={
                    "http": 'http://' + self.proxy,
                    "https": 'http://' + self.proxy

                    # configuramos ssl en el cliente

                }, verify=ssl_context, http2=True)



            else:
             client = httpx.AsyncClient(verify=False,http2=True)


            if self.method == 'get':

                request = client.build_request('get', self.url, headers=collections.OrderedDict(self.headers),
                                               params=self.params)
 

                self.response = await client.send(request,allow_redirects=True)
                await client.aclose()
                return self.response.text


            elif self.method == 'post':


                body = str(self.body)



                request = client.build_request('post', self.url, headers=collections.OrderedDict(self.headers),
                                               params=self.params, data=body)

                request.headers.pop(self.remove)
                self.response = await client.send(request)
                await client.aclose()
                return json.load(self.response)


        except Exception as e:
            print(e.args)
            print(traceback.print_exc())
        finally:
            self.reset()


Output:

Traceback (most recent call last):
  File "C:\Users\qwert\Desktop\HTTP_BOT\ve\lib\site-packages\httpx\_transports\default.py", line 61, in map_httpcore_exceptions
    yield
  File "C:\Users\qwert\Desktop\HTTP_BOT\ve\lib\site-packages\httpx\_transports\default.py", line 281, in handle_async_request
    ) = await self._pool.handle_async_request(
  File "C:\Users\qwert\Desktop\HTTP_BOT\ve\lib\site-packages\httpcore\_async\connection_pool.py", line 237, in handle_async_request
    response = await connection.handle_async_request(
  File "C:\Users\qwert\Desktop\HTTP_BOT\ve\lib\site-packages\httpcore\_async\connection.py", line 136, in handle_async_request
    self.socket = await self._open_socket(timeout)
  File "C:\Users\qwert\Desktop\HTTP_BOT\ve\lib\site-packages\httpcore\_async\connection.py", line 163, in _open_socket
    return await self._backend.open_tcp_stream(
  File "C:\Users\qwert\Desktop\HTTP_BOT\ve\lib\site-packages\httpcore\_backends\auto.py", line 44, in open_tcp_stream
    return await self.backend.open_tcp_stream(
  File "C:\Users\qwert\Desktop\HTTP_BOT\ve\lib\site-packages\httpcore\_backends\anyio.py", line 154, in open_tcp_stream
    stream = await TLSStream.wrap(
  File "C:\Users\qwert\AppData\Local\Programs\Python\Python38\lib\contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\qwert\Desktop\HTTP_BOT\ve\lib\site-packages\httpcore\_exceptions.py", line 12, in map_exceptions
    raise to_exc(exc) from None
httpcore.ConnectTimeout

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\qwert\Desktop\HTTP_BOT\network\http\Client.py", line 95, in run
    self.response = await client.send(request)
  File "C:\Users\qwert\Desktop\HTTP_BOT\ve\lib\site-packages\httpx\_client.py", line 1586, in send
    response = await self._send_handling_auth(
  File "C:\Users\qwert\Desktop\HTTP_BOT\ve\lib\site-packages\httpx\_client.py", line 1616, in _send_handling_auth
    response = await self._send_handling_redirects(
  File "C:\Users\qwert\Desktop\HTTP_BOT\ve\lib\site-packages\httpx\_client.py", line 1655, in _send_handling_redirects
    response = await self._send_single_request(request, timeout)
  File "C:\Users\qwert\Desktop\HTTP_BOT\ve\lib\site-packages\httpx\_client.py", line 1699, in _send_single_request
    ) = await transport.handle_async_request(
  File "C:\Users\qwert\Desktop\HTTP_BOT\ve\lib\site-packages\httpx\_transports\default.py", line 276, in handle_async_request
    (
  File "C:\Users\qwert\AppData\Local\Programs\Python\Python38\lib\contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\qwert\Desktop\HTTP_BOT\ve\lib\site-packages\httpx\_transports\default.py", line 78, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ConnectTimeout
C:\Users\qwert\Desktop\HTTP_BOT\ve\lib\site-packages\httpx\_client.py:2003: UserWarning: Unclosed <httpx.AsyncClient object at 0x000001EEBB440970>. See https://www.python-httpx.org/async/#opening-and-closing-clients for details.
  warnings.warn(
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001EEB9E5B310>
Traceback (most recent call last):
  File "C:\Users\qwert\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\qwert\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\qwert\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 719, in call_soon
    self._check_closed()
  File "C:\Users\qwert\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 508, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed


Seems im not closing correctly my async client? I dont get why or how, im using aclose as the docs tell to.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions