I currently have a python script that runs on my local machine that uploads some files by calling a basic API I built using Flask that's on a remote machine. The script basically iterates thru a list of file paths, checks for some criteria and uploads the file if it meets the criteria. Here is stripped down version of the script for clarity:
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()
The problem is that this script is blocking the uploading of other files as they are detected. They all eventually upload but I need to send each file asynchronously but not simultaneously. I looked into grequests, requests-futures, asyncore, Twisted, etc. Twisted looks like the best option, but will require me learning Twisted and redesigning the script. Any tips/opinions on whether this is the route I should go would be greatly appreciated. Thanks in advance.