Python 多线程实例详解
Python多线程实例详解
多线程通常是新开一个后台线程去处理比较耗时的操作,Python做后台线程处理也是很简单的,今天从官方文档中找到了一个Demo.
实例代码:
importthreading,zipfile
classAsyncZip(threading.Thread):
def__init__(self,infile,outfile):
threading.Thread.__init__(self)
self.infile=infile
self.outfile=outfile
defrun(self):
f=zipfile.ZipFile(self.outfile,'w',zipfile.ZIP_DEFLATED)
f.write(self.infile)
f.close()
print('Finishedbackgroundzipof:',self.infile)
background=AsyncZip('mydata.txt','myarchive.zip')
background.start()
print('Themainprogramcontinuestoruninforeground.')
background.join()#Waitforthebackgroundtasktofinish
print('Mainprogramwaiteduntilbackgroundwasdone.')
结果:
Themainprogramcontinuestoruninforeground. Finishedbackgroundzipof:mydata.txt Mainprogramwaiteduntilbackgroundwasdone. Pressanykeytocontinue...
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西