Skip to content

Instantly share code, notes, and snippets.

@ishahid
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishahid/9028083 to your computer and use it in GitHub Desktop.
Save ishahid/9028083 to your computer and use it in GitHub Desktop.
Dropbox File Transfer Comparison - Queue vs Parallel
#!/usr/bin/python
"""
Dropbox File Transfer Comparison - POC
This program compares the speed of uploading multiple files to dropbox using
both queued and parallel connections per file.
"""
import time
import threading
import dropbox
app_key = 'APP_KEY'
app_secret = 'APP_SECRET'
class UploadThread(threading.Thread):
def __init__(self, client, file, n):
threading.Thread.__init__(self)
self.client = client
self.file = file
self.n = n
def run(self):
f = open(self.file, 'rb')
response = self.client.put_file('/tmp/p%s.jpg' % self.n, f)
print 'uploaded file %s' % self.file
def upload_in_sequence(client):
start_time = time.time()
print ''
print 'uploading files one by one...'
f = open('files/1.jpg', 'rb')
print 'uploading file 1/3...'
response = client.put_file('/tmp/s1.jpg', f)
f = open('files/2.jpg', 'rb')
print 'uploading file 2/3...'
response = client.put_file('/tmp/s2.jpg', f)
f = open('files/3.jpg', 'rb')
print 'uploading file 3/3...'
response = client.put_file('/tmp/s3.jpg', f)
time_taken = time.time() - start_time
print 'time taken (seconds): %s' % round(time_taken, 5)
def upload_in_parallel(client):
start_time = time.time()
print ''
print 'uploading files in parallel...'
thread1 = UploadThread(client, 'files/1.jpg', 1)
thread2 = UploadThread(client, 'files/2.jpg', 2)
thread3 = UploadThread(client, 'files/3.jpg', 3)
thread1.start()
thread2.start()
thread3.start()
thread1.join()
thread2.join()
thread3.join()
time_taken = time.time() - start_time
print 'time taken (seconds): %s' % round(time_taken, 5)
if __name__ == "__main__":
flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)
authorize_url = flow.start()
print '1. Go to: ' + authorize_url
print '2. Click "Allow" (you might have to log in first)'
print '3. Copy the authorization code.'
code = raw_input("Enter the authorization code here: ").strip()
try:
access_token, user_id = flow.finish(code)
client = dropbox.client.DropboxClient(access_token)
print 'linked account: ', client.account_info()['display_name']
upload_in_sequence(client)
upload_in_parallel(client)
except dropbox.rest.ErrorResponse:
print 'invalid authorization code.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment