最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python線程里哪種模塊比較適合

 更新時間:2020年08月02日 09:36:26   作者:yang  
在本篇文章里我們給大家整理了關(guān)于python線程里哪種模塊比較適合的相關(guān)知識點,需要的朋友們可以學(xué)習(xí)下。

在Python中可使用的多線程模塊主要有兩個,thread和threading模塊。thread模塊提供了基本的線程和鎖的支持,建議新手不要使用。threading模塊允許創(chuàng)建和管理線程,提供了更多的同步原語。

thread模塊函數(shù):

  • start_new_thread(function, args[, kwargs]):啟動新的線程以執(zhí)行function,返回線程標識。
  • allocate_lock():返回LockType對象。
  • exit():拋出SystemExit異常,如果沒有被捕獲,線程靜默退出。
  • LockType類型鎖對象的方法:
  • acquire([waitflag]):無參數(shù),無條件獲得鎖,如果鎖已經(jīng)被其他線程獲取,則等待鎖被釋放。如果使用整型參數(shù),參數(shù)為0,如果鎖可獲取,則獲取且返回True,否則返回False;參數(shù)為非0,與無參數(shù)相同。
  • locked():返回鎖的狀態(tài),如果已經(jīng)被獲取,則返回True,否則返回False。
  • release():釋放鎖。只有已經(jīng)被獲取的鎖才能被釋放,不限于同一個線程。
  • threading模塊提供了更好的線程間的同步機制。threading模塊下有如下對象:
  • Thread
  • Lock
  • RLock
  • Condition
  • Event
  • Semaphore
  • BoundedSemaphore
  • Timer
  • threading模塊內(nèi)還有如下的函數(shù):
  • active_count()
  • activeCount():返回當(dāng)前alive的線程數(shù)量
  • Condition():返回新的條件變量對象
  • current_thread()
  • currentThread():返回當(dāng)前線程對象
  • enumerate():返回當(dāng)前活動的線程,不包括已經(jīng)結(jié)束和未開始的線程,包括主線程及守護線程。
  • settrace(func):為所有線程設(shè)置一個跟蹤函數(shù)。
  • setprofile(func):為所有純種設(shè)置一個profile函數(shù)。

內(nèi)容擴展:

Python線程模塊

常用參數(shù)說明

  • target 表示調(diào)用對象,幾子線程要執(zhí)行的的任務(wù)
  • name 子線程的名稱
  • args 傳入target函數(shù)中的位置參數(shù),是一個元組,參數(shù)后必須加逗號

常用的方法

  • Thread.star(self)啟動進程
  • Thread.join(self)阻塞進程,主線程等待
  • Thread.setDaemon(self,daemoic) 將子線程設(shè)置為守護線程
  • Thread.getName(self.name) 獲取線程名稱
  • Thread.setName(self.name) 設(shè)置線程名稱
import time
from threading import Thread
 
 
def hello(name):
  print('hello {}'.format(name))
  time.sleep(3)
  print('hello bye')
 
def hi():
  print('hi')
  time.sleep(3)
  print('hi bye')
 
if __name__ == '__main__':
 
  hello_thread = Thread(target=hello, args=('wan zong',),name='helloname') #target表示調(diào)用對象。name是子線程的名稱。args 傳入target函數(shù)中的位置參數(shù),是個元組,參數(shù)后必須加逗號
  hi_thread = Thread(target=hi)
 
  hello_thread.start() #開始執(zhí)行線程任務(wù),啟動進程
  hi_thread.start()
 
  hello_thread.join() #阻塞進程 等到進程運行完成 阻塞調(diào)用,主線程進行等待
  hi_thread.join()
 
  print(hello_thread.getName())
  print(hi_thread.getName()) #會默認匹配名字
 
  hi_thread.setName('hiname')
  print(hi_thread.getName())
 
  print('主線程運行完成!')

到此這篇關(guān)于python線程里哪種模塊比較適合的文章就介紹到這了,更多相關(guān)python線程用什么模塊好內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

余庆县| 林芝县| 泽库县| 景德镇市| 双江| 孝义市| 同心县| 阳江市| 鹤山市| 南通市| 凉山| 砚山县| 开封市| 澎湖县| 宣武区| 昌乐县| 肃南| 香格里拉县| 利川市| 昌吉市| 五常市| 宁津县| 鱼台县| 钦州市| 利津县| 蒙阴县| 中卫市| 修文县| 射洪县| 定边县| 冀州市| 绥江县| 兴安县| 青神县| 深水埗区| 宁南县| 全椒县| 深州市| 新泰市| 凭祥市| 湾仔区|