歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python 多線程就這麼簡單

Python 多線程就這麼簡單

日期:2017/3/1 9:28:12   编辑:Linux編程

多線程和多進程是什麼自行google補腦

  對於Python 多線程的理解,我花了很長時間,搜索的大部份文章都不夠通俗易懂。所以,這裡力圖用簡單的例子,讓你對多線程有個初步的認識。

單線程

  在好些年前的MS-DOS時代,操作系統處理問題都是單任務的,我想做聽音樂和看電影兩件事兒,那麼一定要先排一下順序。

(好吧!我們不糾結在DOS時代是否有聽音樂和看影的應用。^_^)

from time import ctime,sleep

def music():
for i in range(2):
print "I was listening to music. %s" %ctime()
sleep(1)

def move():
for i in range(2):
print "I was at the movies! %s" %ctime()
sleep(5)

if __name__ == '__main__':
music()
move()
print "all over %s" %ctime()

  我們先聽了一首音樂,通過for循環來控制音樂的播放了兩次,每首音樂播放需要1秒鐘,sleep()來控制音樂播放的時長。接著我們又看了一場電影,

每一場電影需要5秒鐘,因為太好看了,所以我也通過for循環看兩遍。在整個休閒娛樂活動結束後,我通過
print "all over %s" %ctime()

看了一下當前時間,差不多該睡覺了。

運行結果:

>>=========================== RESTART ================================
>>>
I was listening to music. Thu Apr 17 10:47:08 2014
I was listening to music. Thu Apr 17 10:47:09 2014
I was at the movies! Thu Apr 17 10:47:10 2014
I was at the movies! Thu Apr 17 10:47:15 2014
all over Thu Apr 17 10:47:20 2014

  

  其實,music()和move()更應該被看作是音樂和視頻播放器,至於要播放什麼歌曲和視頻應該由我們使用時決定。所以,我們對上面代碼做了改造:

#coding=utf-8
import threading
from time import ctime,sleep

def music(func):
for i in range(2):
print "I was listening to %s. %s" %(func,ctime())
sleep(1)

def move(func):
for i in range(2):
print "I was at the %s! %s" %(func,ctime())
sleep(5)

if __name__ == '__main__':
music(u'愛情買賣')
move(u'阿凡達')

print "all over %s" %ctime()

  對music()和move()進行了傳參處理。體驗中國經典歌曲和歐美大片文化。

運行結果:

>>> ======================== RESTART ================================
>>>
I was listening to 愛情買賣. Thu Apr 17 11:48:59 2014
I was listening to 愛情買賣. Thu Apr 17 11:49:00 2014
I was at the 阿凡達! Thu Apr 17 11:49:01 2014
I was at the 阿凡達! Thu Apr 17 11:49:06 2014
all over Thu Apr 17 11:49:11 2014

多線程

  科技在發展,時代在進步,我們的CPU也越來越快,CPU抱怨,P大點事兒占了我一定的時間,其實我同時干多個活都沒問題的;於是,操作系統就進入了多任務時代。我們聽著音樂吃著火鍋的不在是夢想。

  python提供了兩個模塊來實現多線程thread 和threading ,thread 有一些缺點,在threading 得到了彌補,為了不浪費你和時間,所以我們直接學習threading 就可以了。

繼續對上面的例子進行改造,引入threadring來同時播放音樂和視頻:

#coding=utf-8
import threading
from time import ctime,sleep

def music(func):
for i in range(2):
print "I was listening to %s. %s" %(func,ctime())
sleep(1)

def move(func):
for i in range(2):
print "I was at the %s! %s" %(func,ctime())
sleep(5)

threads = []
t1 = threading.Thread(target=music,args=(u'愛情買賣',))
threads.append(t1)
t2 = threading.Thread(target=move,args=(u'阿凡達',))
threads.append(t2)

if __name__ == '__main__':
for t in threads:
t.setDaemon(True)
t.start()

print "all over %s" %ctime()

import threading

首先導入threading 模塊,這是使用多線程的前提。

threads = []

t1 = threading.Thread(target=music,args=(u'愛情買賣',))

threads.append(t1)

  創建了threads數組,創建線程t1,使用threading.Thread()方法,在這個方法中調用music方法target=music,args方法對music進行傳參。 把創建好的線程t1裝到threads數組中。

  接著以同樣的方式創建線程t2,並把t2也裝到threads數組。

for t in threads:

  t.setDaemon(True)

  t.start()

最後通過for循環遍歷數組。(數組被裝載了t1和t2兩個線程)

setDaemon()

  setDaemon(True)將線程聲明為守護線程,必須在start() 方法調用之前設置,如果不設置為守護線程程序會被無限掛起。子線程啟動後,父線程也繼續執行下去,當父線程執行完最後一條語句print "all over %s" %ctime()後,沒有等待子線程,直接就退出了,同時子線程也一同結束。

start()

開始線程活動。

運行結果:

>>> ========================= RESTART ================================
>>>
I was listening to 愛情買賣. Thu Apr 17 12:51:45 2014 I was at the 阿凡達! Thu Apr 17 12:51:45 2014 all over Thu Apr 17 12:51:45 2014

  從執行結果來看,子線程(muisc 、move )和主線程(print "all over %s" %ctime())都是同一時間啟動,但由於主線程執行完結束,所以導致子線程也終止。

繼續調整程序:

...
if __name__ == '__main__':
for t in threads:
t.setDaemon(True)
t.start()

t.join()

print "all over %s" %ctime()

  我們只對上面的程序加了個join()方法,用於等待線程終止。join()的作用是,在子線程完成運行之前,這個子線程的父線程將一直被阻塞。

  注意: join()方法的位置是在for循環外的,也就是說必須等待for循環裡的兩個進程都結束後,才去執行主進程。

運行結果:

>>> ========================= RESTART ================================
>>>
I was listening to 愛情買賣. Thu Apr 17 13:04:11 2014 I was at the 阿凡達! Thu Apr 17 13:04:11 2014

I was listening to 愛情買賣. Thu Apr 17 13:04:12 2014
I was at the 阿凡達! Thu Apr 17 13:04:16 2014
all over Thu Apr 17 13:04:21 2014

  從執行結果可看到,music 和move 是同時啟動的。

  開始時間4分11秒,直到調用主進程為4分22秒,總耗時為10秒。從單線程時減少了2秒,我們可以把music的sleep()的時間調整為4秒。

...
def music(func):
for i in range(2):
print "I was listening to %s. %s" %(func,ctime())
sleep(4)
...

執行結果:

>>> ====================== RESTART ================================
>>>
I was listening to 愛情買賣. Thu Apr 17 13:11:27 2014I was at the 阿凡達! Thu Apr 17 13:11:27 2014

I was listening to 愛情買賣. Thu Apr 17 13:11:31 2014
I was at the 阿凡達! Thu Apr 17 13:11:32 2014
all over Thu Apr 17 13:11:37 2014

  子線程啟動11分27秒,主線程運行11分37秒。

  雖然music每首歌曲從1秒延長到了4 ,但通多程線的方式運行腳本,總的時間沒變化。

本文從感性上讓你快速理解python多線程的使用,更詳細的使用請參考其它文檔或資料。

==========================================================

無需操作系統直接運行 Python 代碼 http://www.linuxidc.com/Linux/2015-05/117357.htm

CentOS上源碼安裝Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm

==========================================================

class threading.Thread()說明:

class threading.Thread(group=None, target=None, name=None, args=(), kwargs={})

This constructor should always be called with keyword arguments. Arguments are:

  group should be None; reserved for future extension when a ThreadGroup class is implemented.

  target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

  name is the thread name. By default, a unique name is constructed of the form “Thread-N” where N is a small decimal number.

  args is the argument tuple for the target invocation. Defaults to ().

  kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.

If the subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing

anything else to the thread.

更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2015-06/119279p2.htm

Copyright © Linux教程網 All Rights Reserved