Skip to content

Instantly share code, notes, and snippets.

@chrismcfee
Created July 30, 2018 23:07
Show Gist options
  • Save chrismcfee/0d318ee1a0f138aa693ec77f64c61b4b to your computer and use it in GitHub Desktop.
Save chrismcfee/0d318ee1a0f138aa693ec77f64c61b4b to your computer and use it in GitHub Desktop.
multithread.py
import threading, zipfile, time
class AsyncZip(threading.Thread):
def __init__(self, infile, outfile):
threading.Thread.__init__(self)
self.infile = infile
self.outfile = outfile
def run(self):
f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
f.write(self.infile)
f.close()
time.sleep(5)
print 'Finished background zip of: ', self.infile
background = AsyncZip('mydata.txt', 'myarchive.zip')
background.start()
print 'The main program continues to run in foreground.'
#time.sleep(5)
background.join() # Wait for the background task to finish
print 'Main program waited until background was done.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment