详细内容见:http://blog.chedushi.com/archives/9158import requests import datetime from multiprocessing import Pool #适用于cpu密集型(文件处理等大量计算的任务) from multiprocessing.dummy import Pool as ThreadPool #适用于io密集型(网络下载等任务) #可通过试验确定最佳线程数 def main(): urls = [ 'http://www.python.org', 'http://www.python.org/about/', 'http://www.python.org/doc/', 'http://www.python.org/download/', 'http://www.python.org/getit/', 'http://www.python.org/community/', 'http://www.python.org/psf/', 'http://docs.python.org/devguide/', 'http://www.python.org/community/awards/' # etc.. ] pool = ThreadPool(9) # pool = Pool(9) results = pool.map(requests.get, urls) print(results) if __name__ == '__main__': t1=datetime.datetime.now() main() t2=datetime.datetime.now() print(t2-t1)
python 多线程简单应用
最新推荐文章于 2024-01-22 23:32:05 发布