python flask 上传下载 api_使用Python Twisted的Flask API异步文件上传?

我目前有一个在本地机器上运行的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并重新设计脚本。如果您对我是否该走这条路有任何建议/意见,我们将不胜感激。提前谢谢。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值