我正在使用我编写的相当简单的代码从名为ZINC的数据库中下载多个SMI文件。然而,考虑到文件的大小(几kb)和我的互联网连接,它的速度看起来不那么好。 有没有办法加快速度?
import urllib2
def job(url):
''' This function opens the URL and download SMI files from ZINC15'''
u = urllib2.urlopen(url) # Open URL
print 'downloading ' + url # Print which files is being downloaded
with open('output.smi', 'a') as local_file:
local_file.write(u.read())
with open('data.csv') as flist:
urls = ['http://zinc15.docking.org/substances/{}.smi'.format(str(line.rstrip())) for line in flist]
map(job, urls)
答案 0 :(得分:4)
import threading
import Queue # the correct module name is Queue
MAX_THREADS = 10
urls = Queue.Queue()
def downloadFile():
while not urls.empty()
u = urls.get_nowait()
job(u)
for url in your_url_list:
urls.put(url)
for i in range(0, MAX_THREADS + 1):
t = threading.Thread(target=downloadFile)
t.start()
基本上它导入了线程和队列模块,Queu对象将保存要在多个线程中使用的数据,每个线程都将执行downloadFile()函数。
易于理解,如果没有,请告诉我。