python_noob
python_noob

Reputation: 41

Does python's httplib.HTTPConnection block?

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.

Upvotes: 3

Views: 4124

Answers (1)

Mr_Pink
Mr_Pink

Reputation: 109406

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.

Upvotes: 5

Related Questions