Python中thread 多執行緒處理

2021-07-03 02:40:10 字數 3324 閱讀 8595

參考文章:

python模組學習 —- thread 多執行緒處理

python 標準庫提供了 thread 和 threading 兩個模組來對多執行緒進行支援。其中, thread 模組以低階、原始的方式來處理和控制線程,而 threading 模組通過對 thread 進行二次封裝,提供了更方便的 api 來處理執行緒。 雖然使用 thread 沒有 threading 來的方便。

下面是一段簡單的**,在我們自己的機器上執行一下

__author__ = 'cmz'

#coding:utf-8

import time

import thread

import random

count = 0

defthreadtest

():global count

for i in xrange(10000):

count+=1

for i in range(10):

thread.start_new_thread(threadtest,())

time.sleep(3)

print count

第一次執行的結果是:19878

第二次執行的結果是:32257

並沒有得到我們想到的結果

顯然,程式每次執行的結果都是不一樣的,那麼這是為什麼呢?其實就是程式執行的過程中發生了執行緒的搶奪導致的。

解決上面問題的乙個有效的方法就是加鎖,

瑣可以保證在任何時刻,最多只有乙個執行緒可以訪問共享資源。

下面是經過修改時候的程式:

__author__ = 'cmz'

#coding:utf-8

import time

import thread

import random

count = 0

lock = thread.allocate_lock() #建立乙個鎖物件

defthreadtest

():global count ,lock

lock.acquire()#獲取鎖

for i in xrange(10000):

count+=1

lock.release() #釋放鎖

for i in range(10):

thread.start_new_thread(threadtest,())

time.sleep(3)

print count

程式執行的結果是:100000

顯然這次程式執行才是正確的。

下面解釋一下程式中用到的幾個函式:

thread.start_new_thread ( function , args [ , kwargs ] )

函式將建立乙個新的執行緒,並返回該執行緒的識別符號(識別符號為整數)。引數 function 表示執行緒建立之後,立即執行的函式,引數 args 是該函式的引數,它是乙個元組型別;第二個引數 kwargs 是可選的,它為函式提供了命名引數字典。函式執行完畢之後,執行緒將自動退出。如果函式在執行過程中遇到未處理的異常,該執行緒將退出,但不會影響其他執行緒的執行。 下面是乙個簡單的例子:

#coding=utf-8

import thread, time

def threadfunc(a = none, b = none, c = none, d = none):

print

time.strftime('%h:%m:%s', time.localtime()), a

time.sleep(1)

print

time.strftime('%h:%m:%s', time.localtime()), b

time.sleep(1)

print

time.strftime('%h:%m:%s', time.localtime()), c

time.sleep(1)

print

time.strftime('%h:%m:%s', time.localtime()), d

time.sleep(1)

print

time.strftime('%h:%m:%s', time.localtime()), 'over'

thread.start_new_thread(threadfunc, (3, 4, 5, 6))

#建立執行緒,並執行threadfunc函式。

time.sleep(5)'''

執行結果

#17:26:18 3

#17:26:19 4

#17:26:20 5

#17:26:21 6

#17:26:22 over

'''

thread.exit()

結束當前執行緒。呼叫該函式會觸發 systemexit 異常,如果沒有處理該異常,執行緒將結束。

thread.get_ident ()

返回當前執行緒的識別符號,識別符號是乙個非零整數。

thread.interrupt_main ()

在主線程中觸發 keyboardinterrupt 異常。子執行緒可以使用該方法來中斷主線程。下面的例子演示了在子執行緒中呼叫 interrupt_main ,在主線程中捕獲異常:

__author__ = 'cmz'

#coding:utf-8

import thread,time

thread.start_new_thread(lambda :(thread.interrupt_main(),),())

try:

time.sleep(2)

except keyboardinterrupt,e:

print

'error:',e

print

'over'

lock.acquire ( [ waitflag ] )

獲取瑣。函式返回乙個布林值,如果獲取成功,返回 true ,否則返回 false 。引數 waitflag 的預設值是乙個非零整數,表示如果瑣已經被其他執行緒占用,那麼當前執行緒將一直等待,只到其他執行緒釋放,然後獲取訪瑣。如果將引數 waitflag 置為 0 ,那麼當前執行緒會嘗試獲取瑣,不管瑣是否被其他執行緒占用,當前執行緒都不會等待。

lock.release ()

釋放所占用的瑣。

lock.locked ()

判斷瑣是否被占用。

python捕捉執行緒錯誤 python 多執行緒錯誤

我想用多執行緒查詢資料庫,然後進行資料操作。list range 19999,100000 pool threadpool 10 results pool.map main,list pool.close pool.join def main i print i query id,link,keyw...

python中thread執行緒運用

coding utf 8 import thread from time import sleep,ctime loops 4,2 def loop nloop,nsec,lock print start loop nloop,at ctime sleep nsec print loop nloop...

python的thread和threading區別

python提供了多種模組用來支援多執行緒程式設計,thread 在python3中改名為 thread threading,和 queue模組。通過加入queue模組,使用者可以建立多個執行緒共享資料的佇列資料結構。thread和threading模組都可以用來建立和管理執行緒,而thread模組...