Python 多線程實(shí)例詳解
更新時(shí)間:2017年03月25日 16:11:52 投稿:lqh
這篇文章主要介紹了Python 多線程實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
Python 多線程實(shí)例詳解
多線程通常是新開一個(gè)后臺(tái)線程去處理比較耗時(shí)的操作,Python做后臺(tái)線程處理也是很簡單的,今天從官方文檔中找到了一個(gè)Demo.
實(shí)例代碼:
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.')
結(jié)果:
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 . . .
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Python實(shí)現(xiàn)實(shí)時(shí)顯示進(jìn)度條的6種方法
相信大家對(duì)進(jìn)度條一定不陌生了,很多安裝或者下載都會(huì)出現(xiàn)進(jìn)度條,本文主要介紹了Python實(shí)現(xiàn)實(shí)時(shí)顯示進(jìn)度條的6種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2021-12-12
python實(shí)現(xiàn)將中文日期轉(zhuǎn)換為數(shù)字日期
這篇文章主要介紹了python實(shí)現(xiàn)將中文日期轉(zhuǎn)換為數(shù)字日期,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Pygame實(shí)戰(zhàn)之迷宮游戲的實(shí)現(xiàn)
玩迷宮游戲長大的我們,欣慰地看到,下一代仍熱愛著這個(gè)經(jīng)典游戲。本文將通過Python中的Pygame庫制作這一經(jīng)典的游戲,需要的可以參考一下2022-02-02
如何使用?Python?Timedelta?月份計(jì)算日期
在本篇文章中,我們將通過?timedelta?學(xué)習(xí)如何在?Python?中使用日期時(shí)間,?我們將了解如何計(jì)算當(dāng)前日期或任何其他日期六個(gè)月后的日期,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2023-10-10

