Asynchronous Requests with Python requests

前端 未结 12 1397
予麋鹿
予麋鹿 2020-11-22 08:47

I tried the sample provided within the documentation of the requests library for python.

With async.map(rs), I get the response codes, but I want to get

12条回答
  •  孤城傲影
    2020-11-22 09:12

    from threading import Thread
    
    threads=list()
    
    for requestURI in requests:
        t = Thread(target=self.openURL, args=(requestURI,))
        t.start()
        threads.append(t)
    
    for thread in threads:
        thread.join()
    
    ...
    
    def openURL(self, requestURI):
        o = urllib2.urlopen(requestURI, timeout = 600)
        o...
    

提交回复
热议问题