You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add timeout support when calling into python web app #1451
There are no similar issues or pull requests for this yet.
I discussed this idea on the community chat and feedback is positive.
Is your feature related to a problem? Please describe.
I would like to be able to set a timeout when calling into a python web app.
My specific use case is to safe-guard against calls my app makes to external API's or databases which would, without a timeout, can cause the test suite to hang.
I would like to be able to set a timeout in the Client instantiation or request and for that timeout to cancel it's request to the python app if it runs over the timeout.
I've tried adding a middleware to my test app using asyncio.wait_for, but it does not cancel routes that are not async (called by run_in_executor by FastAPI):
I was trying to use "wait_for" to force an specific timeout for my requests and whenever it reaches the timeout it raises an
exception and the connection status stays as ACTIVE forever - forcing a new connection to be created every time and keeping invalid connections at the pool.
You should use "asyncio.wait" instead of "asyncio.wait_for - that way it won't cancel the task and httpx will properly release the connection"
e.g.:
import httpx
import asyncio
async def test():
async with httpx.AsyncClient() as client:
# just to open the first connection and reuse it when calling with wait_for
r = await client.get('https://github.com/encode/httpx')
assert r.status_code == 200
try:
# it must raise a timeout exception
await asyncio.wait_for(client.get('https://github.com/encode/httpx'), timeout=0.1)
except asyncio.exceptions.TimeoutError:
...
finally:
print(client._transport._connections)
#>> {(b'https', b'github.com', 443): {<AsyncHTTPConnection http_version=HTTP/1.1 state=2>}}
# state 2 = ACTIVE
# as you can see, the connection will stay on the pool and won't be reused
loop = asyncio.get_event_loop()
loop.run_until_complete(test())
Calling into python web apps directly should generally be deterministic (and backing services typically ought to be mocked, or locally available). For instance, the Django test client, wouldn't provide this kind of timeout configuration, and I'm not keen on us doing so either.
It sounds like you probably want timeouts around the places you're calling into backing services (rather than around the request itself).
You could alternately customize the behaviour of httpx.ASGITransport (eg. through subclassing it, and wrapping the request up in an overall timeout, then using client = httpx.Client(transport=MyCustomAppTransport(app))
If we're not going to enforce timeouts when using an app, can we raise some sort of exception when a timeout is requested? Silently ignoring the requested timeout seems rather unintuitive to me.
Activity
julianogv commentedon Jan 19, 2021
Be careful when using wait_for + http requests.
I was trying to use "wait_for" to force an specific timeout for my requests and whenever it reaches the timeout it raises an
exception and the connection status stays as ACTIVE forever - forcing a new connection to be created every time and keeping invalid connections at the pool.
You should use "asyncio.wait" instead of "asyncio.wait_for - that way it won't cancel the task and httpx will properly release the connection"
e.g.:
lovelydinosaur commentedon Mar 23, 2021
I can't see us doing this, no.
Calling into python web apps directly should generally be deterministic (and backing services typically ought to be mocked, or locally available). For instance, the Django test client, wouldn't provide this kind of timeout configuration, and I'm not keen on us doing so either.
It sounds like you probably want timeouts around the places you're calling into backing services (rather than around the request itself).
You could alternately customize the behaviour of
httpx.ASGITransport(eg. through subclassing it, and wrapping the request up in an overall timeout, then usingclient = httpx.Client(transport=MyCustomAppTransport(app))patrickkwang commentedon Aug 27, 2021
If we're not going to enforce timeouts when using an app, can we raise some sort of exception when a timeout is requested? Silently ignoring the requested timeout seems rather unintuitive to me.