I am unsure whether or not the following code is a blocking operation in python:
import httplib
import urllib
def do_request(server, port, timeout, remote_url):
conn = httplib.HTTPConnection(server, port, timeout=timeout)
conn.request("POST", remote_url, urllib.urlencode(query_dictionary, True))
conn.close()
return True
do_request("http://www.example.org", 80, 30, "foo/bar")
print "hi!"
And if it is, how would one go about creating a non-blocking asynchronous http request in python?
Thanks from a python noob.
Unless you go to lengths to prevent it, IO will always block.
Although you can do asynchronous requests, you will have to make you entire program async-friendly. Async does not magically make your code non-blocking. It would be much easier to do the request in another thread or process if you don't want to block your main loop.
If you're interested in asynchronous network programming, you will want to look into Twisted.
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