我目前有一个在本地机器上运行的python脚本,它通过调用我在远程机器上使用Flask构建的基本API来上传一些文件。该脚本基本上遍历一个文件路径列表,检查一些条件,并在满足条件时上载文件。为了清晰起见,下面是脚本的精简版本:def upload_file(path):
url = 'http://mydomain.com/upload'
head,tail = os.path.split(path)
files = {'files': (tail, open(path, 'rb'))}
r = requests.post(url,files=files)
return r.status_code
def uploadCallback(status):
if status == 200:
print "file was successfully uploaded, now do something cool"
else:
print "something went wrong"
paths = ['/Users/myFiles/file1.txt', '/Users/myFiles/file2.txt']
meets_criteria = True
for path in paths:
if meets_criteria: #imagine we check to see if its extension is .txt
d = threads.deferToThread(upload_file, path)
d.addCallback(uploadCallback)
else:
pass
reactor.run()
问题是,当检测到其他文件时,此脚本会阻止上载这些文件。他们最终都上传,但我需要发送每个文件异步,但不是同时。我研究了grequests、requests futures、asyncore、Twisted等。Twisted看起来是最好的选择,但需要我学习Twisted并重新设计脚本。如果您对我是否该走这条路有任何建议/意见,我们将不胜感激。提前谢谢。在