Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does python's httplib.HTTPConnection block?

Tags:

python

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.

like image 996
python_noob Avatar asked Jun 11 '26 19:06

python_noob


1 Answers

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.

like image 133
JimB Avatar answered Jun 14 '26 08:06

JimB