python 多執行緒程式設計

2021-09-06 20:53:47 字數 3657 閱讀 1457

一)執行緒基礎

1、建立執行緒:

thread模組提供了start_new_thread函式,用以建立執行緒。start_new_thread函式成功建立後還能夠對其進行操作。

其函式原型:

start_new_thread(function,atgs[,kwargs])

其引數含義例如以下:

args:元組形式的引數列表。

kwargs: 可選引數,以字典的形式指定引數

方法一:通過使用thread模組中的函式建立新執行緒。

>>> import thread >>> def run(n): for i in range(n): print i >>> thread.start_new_thread(run,(4,)) #注意第二個引數一定要是元組的形式 53840 1 >>> 2 3 keyboardinterrupt >>> thread.start_new_thread(run,(2,)) 17840 1 >>> thread.start_new_thread(run,(),) 39720 1 >>> 2 3 thread.start_new_thread(run,(),) 32480 1 >>> 2

方法二:通過繼承threading.thread建立執行緒

>>> import threading >>> class mythread(threading.thread): def __init__(self,num): threading.thread.__init__(self) self.num = num def run(self): #過載run方法 print 'i am', self.num >>> t1 = mythread(1) >>> t2 = mythread(2) >>> t3 = mythread(3) >>> t1.start() #執行執行緒t1 i am >>> 1 t2.start() i am >>> 2 t3.start() i am >>> 3

import threading >>> def run(x,y): for i in range(x,y): print i >>> t1 = threading.thread(target=run,args=(15,20)) #直接使用thread附加函式args為函式引數 >>> t1.start() 15 >>> 16 17 18 19

二)thread物件中的經常用法:

1、isalive方法:

>>> import threading >>> import time >>> class mythread(threading.thread): def __init__(self,id): threading.thread.__init__(self) self.id = id def run(self): time.sleep(5) #休眠5秒 print self.id >>> t = mythread(1) >>> def func(): t.start() print t.isalive() #列印執行緒狀態 >>> func() true >>> 1

2、join方法:

原型:join([timeout]) 

timeout: 可選引數,執行緒執行的最長時間

import threading >>> import time #匯入time模組 >>> class mythread(threading.thread): def __init__(self,id): threading.thread.__init__(self) self.id = id def run(self): x = 0 time.sleep(20) print self.id >>> def func(): t.start() for i in range(5): print i >>> t = mythread(2) >>> func() 0 1 2 3 4 >>> 2 def func(): t.start() t.join() for i in range(5): print i >>> t = mythread(3) >>> func() 3 0 1 2 3 4 >>>

3、執行緒名:

>>> import threading >>> class mythread(threading.thread): def __init__(self,threadname): threading.thread.__init__(self,name=threadname) def run(self): print self.getname() >>> >>> t1 = mythread('t1') >>> t1.start() t1 >>>

4、setdaemon方法

在指令碼執行的過程中有乙個主線程,假設主線程又建立了乙個子執行緒,那麼當主線程退出時,會檢驗子執行緒是否完畢。假設子執行緒未完畢,則主線程會在等待子執行緒完畢後退出。

當須要主線程退出時,無論子執行緒是否完畢都隨主線程退出,則能夠使用thread物件的setdaemon方法來設定。

三)執行緒同步

1.簡單的執行緒同步

使用thread物件的lock和rlock能夠實現簡單的執行緒同步。對於假設須要每次僅僅有乙個執行緒操作的資料,能夠將操作過程放在acquire方法和release方法之間。如:

2、使用條件變數保持執行緒同步。

python的condition物件提供了對複製執行緒同步的支援。使用condition物件能夠在某些事件觸發後才處理資料。condition物件除了具有acquire方法和release的方法外,還有wait方法、notify方法、notifyall方法等用於條件處理。

# -*- coding:utf-8 -*- import threading class producer(threading.thread): def __init__(self,threadname): threading.thread.__init__(self,name = threadname) def run(self): global x con.acquire() if x == 1000000: con.wait() # pass else: for i in range(1000000): x = x + 1 con.notify() print x con.release() class consumer(threading.thread): def __init__(self,threadname): threading.thread.__init__(self,name = threadname) def run(self): global x con.acquire() if x == 0: con.wait() #pass else: for i in range(1000000): x = x - 1 con.notify() print x con.release() con = threading.condition() x = 0 p = producer('producer') c = consumer('consumer') p.start() c.start() p.join() c.join() print x e:/study/python/workspace>xianchengtongbu2.py 1000000 0 0

執行緒間通訊:

event物件用於執行緒間的相互通訊。他提供了設定訊號、清除信巨集、等待等用於實現執行緒間的通訊。

1、設定訊號。event物件使用了set()方法後,isset()方法返回真。

2、清除訊號。使用event物件的clear()方法後,isset()方法返回為假。

3、等待。當event物件的內部訊號標誌為假時,則wait()方法一直等到其為真時才返回。還能夠向wait傳遞引數,設定最長的等待時間。

python 多執行緒程式設計

一 執行緒基礎 1 建立執行緒 thread模組提供了start new thread函式,用以建立執行緒。start new thread函式成功建立後還可以對其進行操作。其函式原型 start new thread function,atgs kwargs 其引數含義如下 args 元組形式的引...

Python多執行緒程式設計

import threading import time deffunc name time.sleep 3 print 子執行緒 s 啟動 threading.current thread name print hello name print 子執行緒 s 結束 threading.curren...

python多執行緒程式設計

本文主要學習的執行緒模組是在python3.7版本環境中的threading模組,不涉及另外的乙個執行緒模組 thread。執行緒模組中的屬性和方法 import threading dir threading 下面列表為返回資料 barrier boundedsemaphore brokenbar...