日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python做中學(五)多線程的用法

系統 1700 0

多線程類似于同時執行多個不同程序,多線程運行有如下優點:

  • 使用線程可以把占據長時間的程序中的任務放到后臺去處理。
  • 用戶界面可以更加吸引人,比如用戶點擊了一個按鈕去觸發某些事件的處理,可以彈出一個進度條來顯示處理的進度。
  • 程序的運行速度可能加快。
  • 在一些等待的任務實現上如用戶輸入、文件讀寫和網絡收發數據等,線程就比較有用了。在這種情況下我們可以釋放一些珍貴的資源如內存占用等等。

每個獨立的線程有一個程序運行的入口、順序執行序列和程序的出口。但是線程不能夠獨立執行,必須依存在應用程序中,由應用程序提供多個線程執行控制。

每個線程都有他自己的一組CPU寄存器,稱為線程的上下文,該上下文反映了線程上次運行該線程的CPU寄存器的狀態。

指令指針和堆棧指針寄存器是線程上下文中兩個最重要的寄存器,線程總是在進程得到上下文中運行的,這些地址都用于標志擁有線程的進程地址空間中的內存。

  • 線程可以被搶占(中斷)。
  • 在其他線程正在運行時,線程可以暫時擱置(也稱為睡眠) -- 這就是線程的退讓。

線程可以分為:

  • 內核線程: 由操作系統內核創建和撤銷。
  • 用戶線程: 不需要內核支持而在用戶程序中實現的線程。

Python3 線程中常用的兩個模塊為:

  • _thread
  • threading(推薦使用)

thread 模塊已被廢棄。用戶可以使用 threading 模塊代替。所以,在 Python3 中不能再使用"thread" 模塊。為了兼容性,Python3 將 thread 重命名為 "_thread"。

threading 模塊除了包含 _thread 模塊中的所有方法外,還提供的其他方法:

  • threading.currentThread(): 返回當前的線程變量。
  • threading.enumerate(): 返回一個包含正在運行的線程的list。正在運行指線程啟動后、結束前,不包括啟動前和終止后的線程。
  • threading.activeCount(): 返回正在運行的線程數量,與len(threading.enumerate())有相同的結果。

除了使用方法外,線程模塊同樣提供了Thread類來處理線程,Thread類提供了以下方法:

  • run(): ?用以表示線程活動的方法。
  • start(): 啟動線程活動。

    ?

  • join([time]): ?等待至線程中止。這阻塞調用線程直至線程的join() 方法被調用中止-正常退出或者拋出未處理的異常-或者是可選的超時發生。
  • isAlive(): ?返回線程是否活動的。
  • getName(): ?返回線程名。
  • setName(): ?設置線程名。

舉個例子:

            
               1
            
            
              #
            
            
              !/usr/bin/python3
            
            
               2
            
            
              #
            
            
               3birds coypright
            
            
               3
            
            
               4
            
            
              import
            
            
               threading

            
            
               5
            
            
              import
            
            
               time

            
            
               6
            
            
               7
            
             exitFlag =
            
               0

            
            
               8
            
            
               9
            
            
              class
            
            
               myThread (threading.Thread):

            
            
              10
            
            
              def
            
            
              __init__
            
            
              (self, threadID, name, counter):

            
            
              11
            
                     threading.Thread.
            
              __init__
            
            
              (self)

            
            
              12
            
                     self.threadIN =
            
               threadID

            
            
              13
            
                     self.name =
            
               name

            
            
              14
            
                     self.counter =
            
               counter

            
            
              15
            
            
              def
            
            
               run(self):

            
            
              16
            
            
              print
            
             (
            
              "
            
            
              3birds start thread:
            
            
              "
            
             +
            
               self.name)

            
            
              17
            
            
                      threadLock.acquire()

            
            
              18
            
                     print_time(self.name, self.counter, 5
            
              )

            
            
              19
            
            
                      threadLock.release()

            
            
              20
            
            
              print
            
             (
            
              "
            
            
              3birds exit thread:
            
            
              "
            
             +
            
               self.name)

            
            
              21
            
            
              22
            
            
              def
            
            
               print_time(threadName, delay, counter):

            
            
              23
            
            
              while
            
            
               counter:

            
            
              24
            
            
              if
            
            
               exitFlag:

            
            
              25
            
            
                          threadName.exit()

            
            
              26
            
            
                      time.sleep(delay)

            
            
              27
            
            
              print
            
             (
            
              "
            
            
              %s: %s
            
            
              "
            
             %
            
               (threadName, time.ctime(time.time())))

            
            
              28
            
                     counter -= 1

            
              29
            
            
              30
            
             threadLock =
            
               threading.Lock()

            
            
              31
            
             threads =
            
               []

            
            
              32
            
            
              33
            
             thread1 = myThread(1, 
            
              "
            
            
              Thread-1
            
            
              "
            
            , 1
            
              )

            
            
              34
            
             thread2 = myThread(2, 
            
              "
            
            
              Thread-2
            
            
              "
            
            , 2
            
              )

            
            
              35
            
            
              36
            
            
              #
            
            
               start new thread
            
            
              37
            
            
              thread1.start()

            
            
              38
            
            
              thread2.start()

            
            
              39
            
            
              thread1.join()

            
            
              40
            
            
              thread2.join()

            
            
              41
            
            
              print
            
            (
            
              "
            
            
              end the thread
            
            
              "
            
            )
          

  運行結果:

            3birds start thread:Thread-1
            
              
3birds start thread:Thread
            
            -2
            
              
Thread
            
            -1: Tue Jul 23 09:08:46 2019
            
              
Thread
            
            -1: Tue Jul 23 09:08:47 2019
            
              
Thread
            
            -1: Tue Jul 23 09:08:48 2019
            
              
Thread
            
            -1: Tue Jul 23 09:08:49 2019
            
              
Thread
            
            -1: Tue Jul 23 09:08:50 2019
            
              
3birds exit thread:Thread
            
            -1
            
              
Thread
            
            -2: Tue Jul 23 09:08:52 2019
            
              
Thread
            
            -2: Tue Jul 23 09:08:54 2019
            
              
Thread
            
            -2: Tue Jul 23 09:08:56 2019
            
              
Thread
            
            -2: Tue Jul 23 09:08:58 2019
            
              
Thread
            
            -2: Tue Jul 23 09:09:00 2019
            
              
3birds exit thread:Thread
            
            -2
            
              
end the thread
            
          

  參考文檔:

1?http://www.runoob.com/python3/python3-multithreading.html


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 新野县| 普洱| 观塘区| 新民市| 于田县| 衢州市| 五指山市| 辰溪县| 开封县| 临安市| 闸北区| 双流县| 保靖县| 托克托县| 二手房| 扶沟县| 安国市| 盐源县| 肇源县| 江门市| 泽州县| 洛宁县| 佛坪县| 阿城市| 兴业县| 板桥市| 霞浦县| 扶绥县| 收藏| 日土县| 开鲁县| 新巴尔虎右旗| 康乐县| 青铜峡市| 故城县| 宝清县| 古蔺县| 武城县| 霍邱县| 哈尔滨市| 彩票|