自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python Library實(shí)際應(yīng)用操作步驟詳解

開(kāi)發(fā) 后端
如果你也在Python Library的實(shí)際應(yīng)用操作步驟中有一些疑惑時(shí),你可以瀏覽我們的文章對(duì)Python Library的相關(guān)知識(shí)有一個(gè)更深的了解。

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

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 來(lái)實(shí)現(xiàn)自己的線程類,或者直接像 C# Thread 那樣傳個(gè)函數(shù)進(jìn)去。

 

  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要實(shí)現(xiàn)自己的線程類,可以重寫(xiě) __init__() 和 run() 就行了。不過(guò)一旦我們定義了 run(),我們傳進(jìn)去的 target 就不會(huì)自動(dòng)執(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 有個(gè)重要的屬性 daemon,和 .NET Thread.IsBackground 是一個(gè)意思,一旦設(shè)置為 Daemon Thread,就表示是個(gè) "后臺(tái)線程"。

 

  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進(jìn)程等待所有前臺(tái)線程退出。

 

  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  

進(jìn)程不等待后臺(tái)線程。

  1. Thread-1 0  
  2. main over! 

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

【編輯推薦】

  1. 用Python小程序建立命令行的實(shí)際應(yīng)用方案
  2. PythonS60手機(jī)中搭建手機(jī)運(yùn)行平臺(tái)的五個(gè)步驟
  3. Python字符串中的mapping的功能介紹
  4. Python web框架在實(shí)際操作過(guò)程中的缺點(diǎn)
  5. Python二維數(shù)組在創(chuàng)建過(guò)程中步驟詳解
責(zé)任編輯:佚名 來(lái)源: 博客園
相關(guān)推薦

2010-03-23 14:54:27

Python目錄文件

2010-03-09 19:07:01

Python語(yǔ)法

2010-03-22 19:11:55

Python連接

2010-03-17 13:14:00

Python Libr

2009-08-25 17:02:20

C#串口操作

2010-12-07 09:20:44

MySQL limit

2010-03-17 12:53:43

Python Libr

2010-03-17 10:01:12

Python安裝

2010-03-17 16:27:39

Python矩陣轉(zhuǎn)置

2010-03-05 13:48:24

Python for

2010-03-12 15:29:19

Pythonexe

2010-03-24 17:03:57

Python源碼分析

2010-06-01 15:54:46

MySQL-pytho

2010-03-17 12:37:51

Python定時(shí)器

2010-03-24 13:17:35

Python嵌入

2010-03-15 16:54:11

Python字典

2010-04-20 11:06:33

Oracle索引

2010-03-22 10:11:28

Python Libr

2010-03-17 14:18:27

Python open

2010-03-25 18:37:28

Python技巧
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)