Asynchronous HTTP calls in Python

后端 未结 4 657
失恋的感觉
失恋的感觉 2020-12-05 15:29

I have a need for a callback kind of functionality in Python where I am sending a request to a webservice multiple times, with a change in the parameter each time. I want t

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 16:13

    Do you know about eventlet? It lets you write what appears to be synchronous code, but have it operate asynchronously over the network.

    Here's an example of a super minimal crawler:

    urls = ["http://www.google.com/intl/en_ALL/images/logo.gif",
         "https://wiki.secondlife.com/w/images/secondlife.jpg",
         "http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif"]
    
    import eventlet
    from eventlet.green import urllib2
    
    def fetch(url):
    
      return urllib2.urlopen(url).read()
    
    pool = eventlet.GreenPool()
    
    for body in pool.imap(fetch, urls):
      print "got body", len(body)
    

提交回复
热议问题