欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python threading多線程編程實例

 更新時間:2014年09月18日 09:53:54   投稿:junjie  
這篇文章主要介紹了Python threading多線程編程實例,本文講解了使用函數(shù)和線程類實現(xiàn)多線程編程的例子,需要的朋友可以參考下

Python 的多線程有兩種實現(xiàn)方法:

函數(shù),線程類

1.函數(shù)

調(diào)用 thread 模塊中的 start_new_thread() 函數(shù)來創(chuàng)建線程,以線程函數(shù)的形式告訴線程該做什么

復(fù)制代碼 代碼如下:

# -*- coding: utf-8 -*-
import thread
def f(name):
  #定義線程函數(shù)
  print "this is " + name
 
if __name__ == '__main__':
  thread.start_new_thread(f, ("thread1",))
  #用start_new_thread()調(diào)用線程函數(shù)和其他參數(shù)
  while 1:
    pass

不過這種方法暫時沒能找到其他輔助方法,連主線程等待都要用 while 1 這種方法解決。

2.線程類

調(diào)用 threading 模塊,創(chuàng)建 threading.Thread 的子類來得到自定義線程類。

復(fù)制代碼 代碼如下:

# -*- coding: utf-8 -*-
import threading
class Th(threading.Thread):
  def __init__(self, name):
    threading.Thread.__init__(self)
    self.t_name = name
    #調(diào)用父類構(gòu)造函數(shù)
 
  def run(self):
    #重寫run()函數(shù),線程默認(rèn)從此函數(shù)開始執(zhí)行
    print "This is " + self.t_name
 
if __name__ == '__main__':
  thread1 = Th("Thread_1")
  thread1.start()
  #start()函數(shù)啟動線程,自動執(zhí)行run()函數(shù)

threading.Thread 類的可繼承函數(shù):
getName() 獲得線程對象名稱
setName() 設(shè)置線程對象名稱
join() 等待調(diào)用的線程結(jié)束后再運(yùn)行之后的命令
setDaemon(bool) 阻塞模式, True: 父線程不等待子線程結(jié)束, False 等待,默認(rèn)為 False
isDaemon() 判斷子線程是否和父線程一起結(jié)束,即 setDaemon() 設(shè)置的值
isAlive() 判斷線程是否在運(yùn)行

實例

復(fù)制代碼 代碼如下:

import threading
import time
class Th(threading.Thread):
  def __init__(self, thread_name):
    threading.Thread.__init__(self)
    self.setName(thread_name)
 
  def run(self):
    print "This is thread " + self.getName()
    for i in range(5):
      time.sleep(1)
      print str(i)
    print self.getName() + "is over"

join() 阻塞等待

復(fù)制代碼 代碼如下:

if __name__ == '__main__':
    thread1 = Th("T1 ")
    thread1.start()
    #thread1.join()
    print "main thread is over"

不帶 thread1.join() ,得到如下結(jié)果:

復(fù)制代碼 代碼如下:

This is thread T1
main thread is over
0
1
2
T1 is over

不等待 thread1 完成,執(zhí)行之后語句。
加了 thread1.join() ,得到如下結(jié)果:
復(fù)制代碼 代碼如下:

This is thread T1
0
1
2
T1 is over
main thread is over

阻塞等待 thread1 結(jié)束,才執(zhí)行下面語句

主線程等待

復(fù)制代碼 代碼如下:

if __name__ == '__main__':
  thread1 = Th("T1 ")
  thread1.setDaemon(True)
  #要在線程執(zhí)行之前就設(shè)置這個量
  thread1.start()
  print "main thread is over"

報錯: Exception in thread T1 (most likely raised during interpreter shutdown):
也就是主線程不等待子線程就結(jié)束了。

多個子線程

復(fù)制代碼 代碼如下:

if __name__ == '__main__':
    for i in range(3):
        t = Th(str(i))
        t.start()
    print "main thread is over"

這里的 t 可同時處理多個線程,即 t 為線程句柄,重新賦值不影響線程。

這里奇怪的是,運(yùn)行 t.run() 時,不會再執(zhí)行其他線程。雖不明,還是用 start() 吧。暫且理解為 start() 是非阻塞并行的,而 run 是阻塞的。

線程鎖

threading 提供線程鎖,可以實現(xiàn)線程同步。

復(fù)制代碼 代碼如下:

import threading
import time
class Th(threading.Thread):
  def __init__(self, thread_name):
    threading.Thread.__init__(self)
    self.setName(thread_name)
 
  def run(self):
    threadLock.acquire()
    #獲得鎖之后再運(yùn)行
    print "This is thread " + self.getName()
    for i in range(3):
      time.sleep(1)
      print str(i)
    print self.getName() + " is over"
    threadLock.release()
    #釋放鎖
if __name__ == '__main__':
  threadLock = threading.Lock()
  #設(shè)置全局鎖
  thread1 = Th('Thread_1')
  thread2 = Th('Thread_2')
  thread1.start()
  thread2.start()

得到結(jié)果:

復(fù)制代碼 代碼如下:

This is thread Thread_1
0
1
2
Thread_1 is over
This is thread Thread_2
0
1
2
Thread_2 is over

相關(guān)文章

  • python爬蟲實戰(zhàn)之爬取京東商城實例教程

    python爬蟲實戰(zhàn)之爬取京東商城實例教程

    這篇文章主要介紹了python爬取京東商城的相關(guān)資料,文中通過爬取一個實例頁面進(jìn)行了講解,通過示例代碼和圖文介紹的非常詳細(xì),相信對大家具有一定的參考價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-04-04
  • python定時關(guān)機(jī)小腳本

    python定時關(guān)機(jī)小腳本

    這篇文章主要為大家詳細(xì)介紹了python定時關(guān)機(jī)小腳本,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 基于Python利用Pygame實現(xiàn)翻轉(zhuǎn)圖像

    基于Python利用Pygame實現(xiàn)翻轉(zhuǎn)圖像

    這篇文章主要介紹了基于Python利用Pygame實現(xiàn)翻轉(zhuǎn)圖像,我們將了解如何使用Pygame翻轉(zhuǎn)圖像,要翻轉(zhuǎn)圖像,我們需要使用pygame.transform.flip(Surface,?xbool,?ybool)?方法,該方法被調(diào)用來根據(jù)我們的需要在垂直方向或水平方向翻轉(zhuǎn)圖像,下面來看看具體的實現(xiàn)過程吧
    2022-02-02
  • 通過gradio和攝像頭獲取照片和視頻實現(xiàn)過程

    通過gradio和攝像頭獲取照片和視頻實現(xiàn)過程

    這篇文章主要為大家介紹了gradio和攝像頭獲取照片和視頻實現(xiàn)過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Tensorflow加載Vgg預(yù)訓(xùn)練模型操作

    Tensorflow加載Vgg預(yù)訓(xùn)練模型操作

    這篇文章主要介紹了Tensorflow加載Vgg預(yù)訓(xùn)練模型操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • Python2和Python3中urllib庫中urlencode的使用注意事項

    Python2和Python3中urllib庫中urlencode的使用注意事項

    這篇文章主要介紹了Python2和Python3中urllib庫中urlencode的使用注意事項,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11
  • 18個Python入門經(jīng)典必背的程序分享

    18個Python入門經(jīng)典必背的程序分享

    這篇文章主要為大家介紹了Python入門經(jīng)典必背的18個程序。注意:這是初學(xué)者要牢記的 18 個代碼,入門之后就簡單了,快跟隨小編一起來學(xué)習(xí)一下吧
    2023-02-02
  • 在windows下使用python進(jìn)行串口通訊的方法

    在windows下使用python進(jìn)行串口通訊的方法

    今天小編就為大家分享一篇在windows下使用python進(jìn)行串口通訊的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Pytorch:Conv2d卷積前后尺寸詳解

    Pytorch:Conv2d卷積前后尺寸詳解

    這篇文章主要介紹了Pytorch:Conv2d卷積前后尺寸,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 解決Python的str強(qiáng)轉(zhuǎn)int時遇到的問題

    解決Python的str強(qiáng)轉(zhuǎn)int時遇到的問題

    下面小編就為大家分享一篇解決Python的str強(qiáng)轉(zhuǎn)int時遇到的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04

最新評論