一.建立執行緒
1.通過thread模組中的start_new_thread(func,args)建立執行緒:
在eclipse+pydev中敲出以下**:
# -*- coding: utf-8 -*-
import thread
def run_thread(n):
for i in range(n):
print i
thread.start_new_thread(run_thread,(4,)) #引數一定是元組,兩個引數可以寫成(a,b)
執行報錯如下:
unhandled exception in thread started by
sys.excepthook is missing
lost sys.stderr
網上查出原因是不建議使用thread,然後我在pythongui中做了測試,測試結果如下,顯然python是支援thread建立多執行緒的,在pydev中出錯原因暫時不明。
>>> import thread
>>> def run(n):
for i in range(n):
print i
>>> thread.start_new_thread(run,(4,))
98520
1>>>
23
2.通過繼承threading.thread建立執行緒,以下示例建立了兩個執行緒
# -*- coding: utf-8 -*-
'''created on 2012-8-8
@author: jeromewei
'''
from threading import thread
import time
class race(thread):
def __init__(self,threadname,interval):
thread.__init__(self,name=threadname)
self.interval = interval
self.isrunning = true
def run(self): #重寫threading.thread中的run()
while self.isrunning:
print 'thread %s is running,time:%s\n' %(self.getname(),time.ctime()) #獲得執行緒的名稱和當前時間
time.sleep(self.interval)
def stop(self):
self.isrunning = false
def test():
thread1 = race('a',1)
thread2 = race('b',2)
thread1.start()
thread2.start()
time.sleep(5)
thread1.stop()
thread2.stop()
if __name__ =='__main__':
test()
3. 在threading.thread中指定目標函式作為執行緒處理函式
# -*- coding: utf-8 -*-
from threading import thread
def run_thread(n):
for i in range(n):
print i
t1 = thread(target=run_thread,args=(5,))#指定目標函式,傳入引數,這裡引數也是元組
t1.start() #啟動執行緒
二. threading.thread中常用函式說明
函式名
功能run()
如果採用方法2建立執行緒就需要重寫該方法
getname()
獲得執行緒的名稱(方法2中有示例)
setname()
設定執行緒的名稱
start()
啟動執行緒
join(timeout)
在join()位置等待另一線程結束後再繼續執行join()後的操作,timeout是可選項,表示最大等待時間
setdaemon(bool)
true:當父執行緒結束時,子執行緒立即結束;false:父執行緒等待子執行緒結束後才結束。預設為false
isdaemon()
判斷子執行緒是否和父執行緒一起結束,即setdaemon()設定的值
isalive()
判斷執行緒是否在執行
以上方法中,我將對join()和setdaemon(bool)作著重介紹,示例如下:
(1)join方法:
# -*- coding: utf-8 -*-
import threading
import time #匯入time模組
class mythread(threading.thread):
def __init__(self,threadname):
threading.thread.__init__(self,name = threadname)
def run(self):
time.sleep(2)
for i in range(5):
print '%s is running····'%self.getname()
t2 = mythread('b')
t2.start()
#t2.join()
for i in range(5):
print 'the program is running···'
這時的程式流程是:主線程先執行完,然後等待b執行緒執行,所以輸出結果為:
the program is running···
the program is running···
the program is running···
b is running····
b is running····
b is running····
如果啟用t2.join(),這時程式的執行流程是:當主線程執行到t2.join()時,它將等待t2執行完,然後再繼續執行t2.join()後的操作,呵呵,你懂了嗎,所以輸出結果為:
b is running····
b is running····
b is running····
the program is running···
the program is running···
the program is running···
(2)setdaemon方法:
# -*- coding: utf-8 -*-
import threading
import time
class mythread(threading.thread):
def __init__(self, threadname):
threading.thread.__init__(self, name=threadname)
def run(self):
time.sleep(5)
print '%s is running·······done'%self.getname()
t=mythread('son thread')
#t.setdaemon(true)
t.start()
if t.isdaemon():
print "the father thread and the son thread are done"
else:
print "the father thread is waiting the son thread····"
這段**的執行流程是:主線程列印完最後一句話後,等待son thread 執行完,然後程式才結束,所以輸出結果為:
the father thread is waitting the son thread····
son thread is running·······done
如果啟用t.setdaemon(true),這段**的執行流程是:當主線程列印完最後一句話後,不管son thread是否執行完,程式立即結束,所以輸出結果為:
the father thread and the son thread are done
三. 小結
介紹到這裡,python多執行緒使用級別的知識點已全部介紹完了,下面我會分析一下python多執行緒的同步問題。
Python多執行緒學習
首先了解一下單執行緒,在啊很多年前的ms dos時代,作業系統處理問題都是單任務的,我想做聽 和看電影兩件事兒,那麼一定要先排一下順序。from time import ctime,sleep defmusic for i in range 2 print i was listening to mu...
python學習 多執行緒
示例 import threading import time def stuthread arg1,arg2 print threading.current thread getname 開始執行 print 引數為 s s arg1,arg2 time.sleep 1 暫停1s print th...
python學習 多執行緒
死鎖概念 程式執行的最小單元,依賴於程序 特點 建立 threading模組thread類建立乙個物件 import threading t threading.thread target 函式名,args 引數 t.start 互斥鎖 建立 threading模組lock類建立乙個物件import...