Python - getting multiple URLs -
i'm coding api endpoint, can think aggregator of other apis. send parameters , translate parameters underlying api's parameters , makes calls. ideally wouldn't want make these calls in serial manner.
what best way this?
edit: suspected, threads or processes isn't way go when thing you're trying getting urls. because of time spent waiting network reply, want change manage changing between tasks waiting tasks doing requests. because of this, think answers exist similar question bad answers.
after research, far can tell single-threaded asynchronous code better answer threads specific case of getting several urls, , case of many many urls:
there's twisted, framework: http://twistedmatrix.com/documents/14.0.1/web/howto/client.html
and gevent, library: http://sdiehl.github.io/gevent-tutorial/
simple example http://mauveweb.co.uk/posts/2014/07/gevent-asynchronous-io-made-easy.html, doing 100 calls using pool of 20:
from gevent import monkey monkey.patch_all() import urllib2 gevent.pool import pool def download(url): return urllib2.urlopen(url).read() if __name__ == '__main__': urls = ['http://httpbin.org/get'] * 100 pool = pool(20) print pool.map(download, urls)
Comments
Post a Comment