-
Notifications
You must be signed in to change notification settings - Fork 32
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
Async callbacks feature request #43
Comments
Hello @calebho , Let me know if I understand your issue properly. You want your callback to trigger a timeout, not issued by httpx, but by trio so it can be catched by trio.move_on_after. Right? And you cannot use time.sleep because it would not trigger the trio timeout. So you need an async call to trio.sleep instead. Am I correct? |
Yep that's right! |
Just to be sure it is actually something that can occur, this is not already handled in some way by httpx when you specify a timeout? |
Not sure I understand fully, but do you mean rewriting my code to use the async def foo():
async with httpx.AsyncClient() as c:
try:
await c.post(endpoint, json=payload, timeout=timeout)
except httpx.TimeoutException:
... # do stuff
else:
... # do other stuff Test: async def test_foo_timeout(httpx_mock, autojump_clock):
def cb(*args):
raise httpx.TimeoutException()
httpx_mock.add_callback(cb)
foo()
... # assertions about what happens if the request times out I think this is a workable solution for this simple case. I think there are slightly more complex cases where something like this could be desirable. For instance suppose there was some other operation which you want to wrap in a timeout: async def foo():
async with httpx.AsyncClient() as c:
with trio.move_on_after(timeout) as cs:
async with trio.open_nursery() as n:
n.start_soon(c.post, endpoint, json=payload)
n.start_soon(some_other_long_running_op)
if cs.cancelled_caught:
... # do stuff
else:
... # do other stuff It becomes a bit ugly to mix different timeout mechanisms ( Anyways, I think my original problem can be addressed without async callbacks, so feel free to close this out if adding this feature would take too much effort |
Indeed that was my question. If your concern can be addressed by I would like to keep code as clean as possible, and for that I would like to avoid adding unnecessary features. But if you are in the second use case, or if you ever encounter it, feel free to re-open this or another ticket. Best Regards |
Hello @calebho, Release 0.21.1 is now available on pypi and allows you to register async callbacks. |
I have a potential use case for an async callback which was originally mentioned in #38. I have the following (pseudo-)code which I want to test:
This is the test function I want to write
Importantly,
autojump_clock
virtualizes the clock so that my test executes immediately. It requires use oftrio.sleep
instead of other sleep functions, e.g.time.sleep
.Alternatively, I could inject a
post
function tofoo
and avoid mocking altogether, i.e.In which case my test would become
Let me know if this makes sense or there is another alternative which I didn't consider.
The text was updated successfully, but these errors were encountered: