On 03/07/2010 05:53 PM, Ping wrote:
> Hi,
> 
> I'm trying to find a way to create an asynchronous HTTP client so I
> can get responses from web servers in a way like
> 
>   async_http_open('http://example.com/', callback_func)
>   # immediately continues, and callback_func is called with response
> as arg when it is ready
> 
> It seems twisted can do it, but I hesitate to bring in such a big
> package as a dependency because my client should be light. Asyncore
> and asynchat are lighter but they don't speak HTTP. The asynchttp
> project on sourceforge is a fusion between asynchat and httplib, but
> it hasn't been updated since 2001 and is seriously out of sync with
> httplib.
> 
> I'd appreciate it if anyone can shed some lights on this.

If you want something quite lightweight, you can spawn a thread for the
call:

import threading, urllib
class AsyncOpen(threading.Thread):
    def __init__(self, url, callback):
        super(AsyncOpen, self).__init__()
        self.url = url
        self.callback = callback
    def run(self):
        # of course change urllib to httplib-something-something
        content = urllib.urlopen(self.url).read()
        self.callback(content)

def asyncopen(url, callback):
    AsyncOpen(url, callback).start()

def cb(content):
    print content

asyncopen('http://www.google.com', cb)
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to