I'm reading the documentation for asynchronous fetch requests in GAE. Python isn't my first language, so I'm having trouble finding out what would be best for my case. I don't really need or care about the response for the request, I just need it to send the request and forget about it and move on to other tasks.
So I tried code like in the documentation:
from google.appengine.api import urlfetch
rpc = urlfetch.create_rpc()
urlfetch.make_fetch_call(rpc, "http://www.google.com/")
# ... do other things ...
try:
result = rpc.get_result()
if result.status_code == 200:
text = result.content
# ...
except urlfetch.DownloadError:
# Request timed out or failed.
# ...
But this code doesn't work unless I include try:
and except
, which I really don't care for. Omitting that part makes the request not go through though.
What is the best option for creating fetch requests where I don't care for the response, so that it just begins the request, and moves on to whatever other tasks there are, and never looks back?
Just do your tasks where the
# ... do other things ...
comment is. When you are otherwise finished, then call rpc.wait()
. Note that it's not the try/except
that's making it work, it's the get_result()
call. That can be replaced with wait()
.
So your code would look like this:
from google.appengine.api import urlfetch
rpc = urlfetch.create_rpc()
urlfetch.make_fetch_call(rpc, "http://www.google.com/")
# ... do other things ... << YOUR CODE HERE
rpc.wait()
If you don't care about the response, the response may take a while, and you don't want your handler to wait for it to complete before returning a response to the user, you may want to consider firing off a task queue task that makes the request rather than doing it within your user-facing handler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With