I'd like to send an http request, and then operate on its response:
import requests
s=requests.Session()
....
(session setup...)
....
(url and params setup...)
....
r=s.get(url,params=params)
print('response json:'+str(r.json()))
In that case, the request should be blocking, so that the print line doesn't happen until a response has been received.
At other times, I'd like to send fire-and-forget requests - the response is never used, so, I don't care how long it takes to be received:
s.post(url,data=data,timeout=0)
What are the options for providing for both cases (blocking vs. fire-and-forget) with an intermittent network connection?
'retry' (with exponential back-off timing or such - as in the tenacity module) works in a blocking manner, but, this question isn't for a web-scraping or large-number-of-requests context.
I do have a functioning queue-and-thread technique in place: submit the request to a Queue instance, and then set a threading.Event to tell the worker thread to start working through the queue, with a delay between non-successful iterations, with each queue item not being removed from the queue until a valid response is received. This works nicely for fire-and-forget, but, it doesn't work in the blocking case, because the timing of the valid response can't be known, so the main thread has likely moved on before the response is available.
asyncio (instead of threading) could be made to either fire-and-forget or block, but I'm trying to find the technique that would be the most clear for the downstream coder.
E.g. should the default be blocking, or, should the default be fire-and-forget?
It's easy enough to specify blocking-or-fire-and-forget with an argument, but the downstream coder could be blindsided when they expect it to be one way and it's actually the other.
Is there a way to specify a 'group' of lines of code that should be a 'blocking set'?
While it would be great to be able to tell if the return value is "used", so that the code could 'automatically' determine if a call should be blocking or not, the answers to this previous question point out that it's not really possible to do so reliably.