encode / httpx

A next generation HTTP client for Python. 🦋

Home Page:https://www.python-httpx.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

httpcore dependency issue with httpx 0.25.0 [AttributeError: 'AsyncLock' object has no attribute '_anyio_lock']

chamikabm opened this issue · comments

The starting point for issues should usually be a discussion...

I've use the following requirements with my project:

requirements.txt
aiohttp==3.8.1 aiohttp-cors==0.7.0 aiohttp-graphql==1.1.0 aiopg==1.3.3 aioredis==2.0.1 aioredis-cluster==2.5.0 aiosignal==1.2.0 alembic==1.7.5 aniso8601==7.0.0 area==1.1.1 asgiref==3.2.7 asn1crypto==1.5.1 async-timeout==4.0.2 asyncstdlib==3.10.3 attrs==19.3.0 autobahn==20.12.3 Automat==20.2.0 bcrypt==3.2.2 boto3==1.20.46 botocore==1.23.46 cffi==1.15.1 channels==2.4.0 chardet==3.0.4 charset-normalizer==2.0.11 colorama==0.4.4 constantly==15.1.0 cryptography==39.0.1 daphne==2.5.0 decorator==5.1.1 deeputil==0.2.10 Deprecated==1.2.13 Django==4.1.9 docutils==0.18.1 frozenlist==1.3.0 GeoAlchemy2==0.13.2 geojson==3.0.1 graphene==2.1.9 graphene-django==2.15.0 graphene-pynamodb==2.3.0 graphene-sqlalchemy==2.2.2 graphene-sqlalchemy-filter==1.13.0 graphene-subscriptions==1.0.2 graphql-core==2.3.2 graphql-relay==2.0.1 graphql-server-core==1.2.0 graphql-ws==0.3.1 greenlet==1.1.2 gunicorn==20.1.0 h11~=0.14.0 h2==3.2.0 h3==3.7.3 hpack==3.0.0 http3==0.6.7 hyperframe==5.2.0 hyperlink==21.0.0 incremental==21.3.0 jmespath==0.10.0 jwcrypto==1.4 Mako==1.2.2 MarkupSafe==2.0.1 multidict==4.7.6 numpy==1.22.2 packaging==21.3 pandas==1.3.4 paramiko==2.11.0 prometheus-client==0.13.1 promise==2.3 psycopg2-binary==2.9.1 pyasn1==0.4.8 pyasn1-modules==0.2.8 pycparser==2.21 PyHamcrest==2.0.3 PyJWT==2.4.0 PyNaCl==1.5.0 pynamodb==5.0.0 pyOpenSSL==23.0.0 pyparsing==3.0.7 pytest-runner==5.3.1 python-dateutil==2.8.2 python-editor==1.0.4 pytz==2021.3 PyYAML==6.0 redis==4.5.5 redshift-connector==2.0.907 repoze.lru==0.7 requests==2.27.1 requests-async==0.6.2 rfc3986==1.5.0 Rx==1.6.1 Rx3==3.0.1 s3transfer==0.5.0 scipy==1.8.0 scramp==1.4.1 service-identity==21.1.0 Shapely==1.8.0 singledispatch==3.7.0 six==1.16.0 SQLAlchemy==1.4.44 SQLAlchemy-bulk-lazy-loader==0.10.0 sqlalchemy-redshift==0.8.11 sqlparse==0.4.4 sshtunnel==0.4.0 structlog==21.5.0 text-unidecode==1.3 trafaret==2.1.0 trafaret-config==2.0.2 Twisted==22.4.0 txaio==21.2.1 typing==3.7.4.3 typing_extensions==4.0.1 ujson==5.4.0 Unidecode==1.3.2 urllib3==1.26.17 Werkzeug==2.2.3 wrapt==1.13.3 yarl==1.7.2 zope.interface==5.4.0 pytest~=5.4.1 httpx==0.25.0 sniffio==1.3.0 certifi==2023.7.22 idna==3.4 httpcore==0.18.0 anyio>=3.0,<5.0


Relevant Code:

`
async def async_http_request(self, http_method: Callable, *args, **kwargs) -> any:
"""
Generic method for async HTTP calls, includes error handling.

    Parameters
    ----------
    http_method: Callable
        HTTP function, e.g., httpx.get or httpx.post.
    args:
        Any positional arguments for the async HTTP function.
    kwargs:
        Any keyword arguments for the async HTTP function. Optionally provide
        timeout argument, defaults to 10 seconds.

    Returns
    -------
    any
        Usually a response type object from the provided async HTTP function.
    """
    if 'timeout' not in kwargs:
        kwargs['timeout'] = 10

    try:
        async with httpx.AsyncClient() as client:
            if http_method is httpx.get:
                httpx_client = client.get
            elif http_method is httpx.post:
                httpx_client = client.post
            else:
                raise ValueError("Unsupported HTTP method")
            resp = await httpx_client(*args, **kwargs)
    except httpx.TimeoutException:
        raise GraphQLError(
            f"Timeout while connecting to '{kwargs.get('url')}'.",
            extensions=GraphQLException.REQUEST_TIMED_OUT.value
        )
    except self.HTTPX_EXCEPTIONS:
        raise GraphQLError(
            f"The '{kwargs.get('url')}' endpoint could not be reached.",
            extensions=GraphQLException.REQUEST_FAILED.value
        )

    if resp.status_code != 200:
        raise GraphQLError(
            resp.text, extensions=GraphQLException.REQUEST_FAILED.value
        )

    return resp

async def async_get_request(self, url: str, params=None, timeout=10, **kwargs):
    """Sends an async GET request and returns response if successful."""
    return await self.async_http_request(httpx.get, url=url, params=params, timeout=timeout, **kwargs)

async def async_post_request(self, url: str, data_payload=None, json_payload=None, timeout=10, **kwargs):
    """Sends an async POST request and returns response if successful."""
    return await self.async_http_request(httpx.post, url=url, data=data_payload, json=json_payload, timeout=timeout,
                                         **kwargs)`

This happens only when we use the dockerized version of the app, locally with PyCharm it works without an issue. It seems like with docker it is unable to load anyio package through httpcore.

Screenshot 2023-10-30 at 10 43 23 am


Error:

graphql-async-test-service | ERROR:graphql_service.components.location_reference_service.queries.location_reference_service:>>> THREAD 281473214135600 - PID 22 - ERROR: 'LrsQuery.resolve_getLocationReferenceDataForGeodetic graphql-async-test-service | Traceback (most recent call last): graphql-async-test-service | File "/app/graphql_service/manager_libraries/base.py", line 119, in async_http_request graphql-async-test-service | resp = await httpx_client(*args, **kwargs) graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1848, in post graphql-async-test-service | return await self.request( graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1530, in request graphql-async-test-service | return await self.send(request, auth=auth, follow_redirects=follow_redirects) graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1617, in send graphql-async-test-service | response = await self._send_handling_auth( graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1645, in _send_handling_auth graphql-async-test-service | response = await self._send_handling_redirects( graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1682, in _send_handling_redirects graphql-async-test-service | response = await self._send_single_request(request) graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1719, in _send_single_request graphql-async-test-service | response = await transport.handle_async_request(request) graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_transports/default.py", line 366, in handle_async_request graphql-async-test-service | resp = await self._pool.handle_async_request(req) graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpcore/_async/connection_pool.py", line 224, in handle_async_request graphql-async-test-service | async with self._pool_lock: graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpcore/_synchronization.py", line 48, in __aenter__ graphql-async-test-service | self.setup() graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpcore/_synchronization.py", line 41, in setup graphql-async-test-service | raise RuntimeError( graphql-async-test-service | RuntimeError: Running under asyncio requires the 'anyio' package to be installed. graphql-async-test-service | graphql-async-test-service | During handling of the above exception, another exception occurred: graphql-async-test-service | graphql-async-test-service | Traceback (most recent call last): graphql-async-test-service | File "/app/graphql_service/utilities/threading_decorators.py", line 127, in add_traceback graphql-async-test-service | return await coro(*args, **kwargs) graphql-async-test-service | File "/app/graphql_service/components/location_reference_service/queries/location_reference_service.py", line 75, in resolve_getLocationReferenceDataForGeodetic graphql-async-test-service | return await location_reference_service_manager.get_lrs_data( graphql-async-test-service | File "/app/graphql_service/manager_libraries/location_reference_service.py", line 71, in get_lrs_data graphql-async-test-service | resp = await self.async_post_request(url=self.get_lrs_data_endpoint, json_payload=payload, timeout=30) graphql-async-test-service | File "/app/graphql_service/manager_libraries/base.py", line 144, in async_post_request graphql-async-test-service | return await self.async_http_request(httpx.post, url=url, data=data_payload, json=json_payload, timeout=timeout, graphql-async-test-service | File "/app/graphql_service/manager_libraries/base.py", line 119, in async_http_request graphql-async-test-service | resp = await httpx_client(*args, **kwargs) graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 2003, in __aexit__ graphql-async-test-service | await self._transport.__aexit__(exc_type, exc_value, traceback) graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_transports/default.py", line 345, in __aexit__ graphql-async-test-service | await self._pool.__aexit__(exc_type, exc_value, traceback) graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpcore/_async/connection_pool.py", line 327, in __aexit__ graphql-async-test-service | await self.aclose() graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpcore/_async/connection_pool.py", line 312, in aclose graphql-async-test-service | async with self._pool_lock: graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpcore/_synchronization.py", line 53, in __aenter__ graphql-async-test-service | await self._anyio_lock.acquire() graphql-async-test-service | AttributeError: 'AsyncLock' object has no attribute '_anyio_lock'


If needed, I can provide the dockerized application to test.