Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Requests: Don't wait for request to finish

In Bash, it is possible to execute a command in the background by appending &. How can I do it in Python?

while True:     data = raw_input('Enter something: ')      requests.post(url, data=data) # Don't wait for it to finish.     print('Sending POST request...') # This should appear immediately. 
like image 537
octosquidopus Avatar asked Nov 19 '14 16:11

octosquidopus


People also ask

Does Python requests wait for response?

The requests. get function is a blocking call. It will wait until the response arrives before the rest of your program will execute.

How do you make Python requests faster?

Use Python's synchronous, multi-threading, queue, and asyncio event loop to make 100 HTTP requests and see which solution performs the best. It is easy to send a single HTTP request by using the requests package.

What is Python requests default timeout?

What is the default timeout for Python requests? None. There is no default timeout for Python requests, unless explicitly set using the timeout parameter.

Is Python request blocking?

Like urllib2 , requests is blocking. But I wouldn't suggest using another library, either. The simplest answer is to run each request in a separate thread. Unless you have hundreds of them, this should be fine.


1 Answers

Here's a hacky way to do it:

try:     requests.get("http://127.0.0.1:8000/test/",timeout=0.0000000001) except requests.exceptions.ReadTimeout:      pass 
like image 155
keithhackbarth Avatar answered Sep 24 '22 22:09

keithhackbarth