Skip to content

Commit 4d85fcc

Browse files
Modernize AsyncZip class to use super() and context manager for file handling
1 parent f27e91e commit 4d85fcc

1 file changed

Lines changed: 3 additions & 4 deletions

File tree

Doc/tutorial/stdlib2.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,13 @@ tasks in background while the main program continues to run::
178178

179179
class AsyncZip(threading.Thread):
180180
def __init__(self, infile, outfile):
181-
threading.Thread.__init__(self)
181+
super().__init__()
182182
self.infile = infile
183183
self.outfile = outfile
184184

185185
def run(self):
186-
f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
187-
f.write(self.infile)
188-
f.close()
186+
with zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED) as f:
187+
f.write(self.infile)
189188
print('Finished background zip of:', self.infile)
190189

191190
background = AsyncZip('mydata.txt', 'myarchive.zip')

0 commit comments

Comments
 (0)