tags:

views:

177

answers:

1

Hello, 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.

+1  A: 

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.

JimB
or the Tornado Project.
Lee
@Lee - Tornado is a small, event based server and web framework. Although you could probably shoehorn the above example into its event loop, it's not really suited for this. (I like Tornado, and have used it for a couple small projects)
JimB