日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
PythonLibrary實際應(yīng)用操作步驟詳解

在Python Library的實際操作過程中,如果你對Python Library的實際操作步驟不是很了解的話,你可以通過我們的文章,對其有一個更好的了解,希望你通過我們的文章,會對你在此方面額知識有所提高。
Python 2.6.4 Standard Library 提供了 thread 和 threading 兩個 Module,其中 thread 明確標明了如下文字。

The thread module has been renamed to _thread in Python 3.0. The 2to3 tool will automatically adapt imports when converting your sources to 3.0; however, you should consider using the high-level threading module instead.

1. Thread

我們可以選擇繼承 Thread 來實現(xiàn)自己的線程類,或者直接像 C# Thread 那樣傳個函數(shù)進去。

 
 
 
  1. from threading import *  
  2. def t(a, b):  
  3. print currentThread().name, a, b  
  4. Thread(ttarget = t, args = (1, 2)).start()  

輸出:

 
 
 
  1. $ ./main.py  
  2. Thread-1 1 2  

Python Library要實現(xiàn)自己的線程類,可以重寫 __init__() 和 run() 就行了。不過一旦我們定義了 run(),我們傳進去的 target 就不會自動執(zhí)行了。

 
 
 
  1. class MyThread(Thread):  
  2. def __init__(self, name, x):  
  3. Thread.__init__(self, namename=name)  
  4. self.x = x  
  5. def run(self):  
  6. print currentThread().name, self.x  
  7. MyThread("My", 1234).start()   

輸出:

 
 
 
  1. $ ./main.py  
  2. My 1234   

Thread 有個重要的屬性 daemon,和 .NET Thread.IsBackground 是一個意思,一旦設(shè)置為 Daemon Thread,就表示是個 "后臺線程"。

 
 
 
  1. def test():  
  2. for i in range(10):  
  3. print currentThread().name, i  
  4. sleep(1)  
  5. t = Thread(target = test)  
  6. #t.daemon = True 
  7. t.start()  
  8. print "main over!"   

輸出:

 
 
 
  1. $ ./main.py  

非 Daemon 效果,Python Library進程等待所有前臺線程退出。

 
 
 
  1. Thread-1 0  
  2. main over!  
  3. Thread-1 1  
  4. Thread-1 2  
  5. Thread-1 3  
  6. Thread-1 4  
  7. Thread-1 5  
  8. Thread-1 6  
  9. Thread-1 7  
  10. Thread-1 8  
  11. Thread-1 9  
  12. $ ./main.py # IsDaemon  

進程不等待后臺線程。

 
 
 
  1. Thread-1 0  
  2. main over! 

以上文章就是對Python Library的實際應(yīng)用操作步驟的介紹。


標題名稱:PythonLibrary實際應(yīng)用操作步驟詳解
URL地址:http://www.dlmjj.cn/article/dpegeio.html