Python 多线程实例详解
更新时间:2017年03月25日 16:11:52 投稿:lqh
这篇文章主要介绍了Python 多线程实例详解的相关资料,需要的朋友可以参考下
Python 多线程实例详解
多线程通常是新开一个后台线程去处理比较耗时的操作,Python做后台线程处理也是很简单的,今天从官方文档中找到了一个Demo.
实例代码:
import threading, zipfile 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() print('Finished background zip of:', self.infile) background = AsyncZip('mydata.txt', 'myarchive.zip') background.start() print('The main program continues to run in foreground.') background.join() # Wait for the background task to finish print('Main program waited until background was done.')
结果:
The main program continues to run in foreground. Finished background zip of: mydata.txt Main program waited until background was done. Press any key to continue . . .
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
详解用Pytest+Allure生成漂亮的HTML图形化测试报告
这篇文章主要介绍了详解用Pytest+Allure生成漂亮的HTML图形化测试报告,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-03-03Python wxPython库Core组件BoxSizer用法示例
这篇文章主要介绍了Python wxPython库Core组件BoxSizer用法,结合实例形式分析了wxPython BoxSizer布局管理相关使用方法及操作注意事项,需要的朋友可以参考下2018-09-09玩转python selenium鼠标键盘操作(ActionChains)
这篇文章主要为大家详细介绍了python selenium鼠标键盘操作(ActionChains),教大家如何玩转selenium鼠标键盘,感兴趣的小伙伴们可以参考一下2016-09-09
最新评论