亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

理解python多線(xiàn)程(python多線(xiàn)程簡(jiǎn)明教程)

 更新時(shí)間:2014年06月09日 10:21:03   作者:  
這篇文章主要介紹了理解python多線(xiàn)程,一個(gè)快速理解python多線(xiàn)程的簡(jiǎn)明教程,需要的朋友可以參考下

對(duì)于python 多線(xiàn)程的理解,我花了很長(zhǎng)時(shí)間,搜索的大部份文章都不夠通俗易懂。所以,這里力圖用簡(jiǎn)單的例子,讓你對(duì)多線(xiàn)程有個(gè)初步的認(rèn)識(shí)。

單線(xiàn)程

  在好些年前的MS-DOS時(shí)代,操作系統(tǒng)處理問(wèn)題都是單任務(wù)的,我想做聽(tīng)音樂(lè)和看電影兩件事兒,那么一定要先排一下順序。

(好吧!我們不糾結(jié)在DOS時(shí)代是否有聽(tīng)音樂(lè)和看影的應(yīng)用。^_^)

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

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()

我們先聽(tīng)了一首音樂(lè),通過(guò)for循環(huán)來(lái)控制音樂(lè)的播放了兩次,每首音樂(lè)播放需要1秒鐘,sleep()來(lái)控制音樂(lè)播放的時(shí)長(zhǎng)。接著我們又看了一場(chǎng)電影,

每一場(chǎng)電影需要5秒鐘,因?yàn)樘每戳耍晕乙餐ㄟ^(guò)for循環(huán)看兩遍。在整個(gè)休閑娛樂(lè)活動(dòng)結(jié)束后,我通過(guò)

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

print "all over %s" %ctime()

看了一下當(dāng)前時(shí)間,差不多該睡覺(jué)了。

運(yùn)行結(jié)果:

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

>>=========================== 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

其實(shí),music()和move()更應(yīng)該被看作是音樂(lè)和視頻播放器,至于要播放什么歌曲和視頻應(yīng)該由我們使用時(shí)決定。所以,我們對(duì)上面代碼做了改造:

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

#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'愛(ài)情買(mǎi)賣(mài)')
    move(u'阿凡達(dá)')

    print "all over %s" %ctime()

對(duì)music()和move()進(jìn)行了傳參處理。體驗(yàn)中國(guó)經(jīng)典歌曲和歐美大片文化。

運(yùn)行結(jié)果:

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

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

多線(xiàn)程

科技在發(fā)展,時(shí)代在進(jìn)步,我們的CPU也越來(lái)越快,CPU抱怨,P大點(diǎn)事兒占了我一定的時(shí)間,其實(shí)我同時(shí)干多個(gè)活都沒(méi)問(wèn)題的;于是,操作系

統(tǒng)就進(jìn)入了多任務(wù)時(shí)代。我們聽(tīng)著音樂(lè)吃著火鍋的不在是夢(mèng)想。

  python提供了兩個(gè)模塊來(lái)實(shí)現(xiàn)多線(xiàn)程thread 和threading ,thread 有一些缺點(diǎn),在threading 得到了彌補(bǔ),為了不浪費(fèi)你和時(shí)間,所以我們直

接學(xué)習(xí)threading 就可以了。

繼續(xù)對(duì)上面的例子進(jìn)行改造,引入threadring來(lái)同時(shí)播放音樂(lè)和視頻:

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

#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'愛(ài)情買(mǎi)賣(mài)',))
threads.append(t1)
t2 = threading.Thread(target=move,args=(u'阿凡達(dá)',))
threads.append(t2)

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

    print "all over %s" %ctime()

import threading

首先導(dǎo)入threading 模塊,這是使用多線(xiàn)程的前提。

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

threads = []
t1 = threading.Thread(target=music,args=(u'愛(ài)情買(mǎi)賣(mài)',))
threads.append(t1)

  創(chuàng)建了threads數(shù)組,創(chuàng)建線(xiàn)程t1,使用threading.Thread()方法,在這個(gè)方法中調(diào)用music方法target=music,args方法對(duì)music進(jìn)行傳參。 把創(chuàng)

建好的線(xiàn)程t1裝到threads數(shù)組中。

接著以同樣的方式創(chuàng)建線(xiàn)程t2,并把t2也裝到threads數(shù)組。


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

for t in threads:
  t.setDaemon(True)
  t.start()

最后通過(guò)for循環(huán)遍歷數(shù)組。(數(shù)組被裝載了t1和t2兩個(gè)線(xiàn)程)

setDaemon()

  setDaemon(True)將線(xiàn)程聲明為守護(hù)線(xiàn)程,必須在start() 方法調(diào)用之前設(shè)置,如果不設(shè)置為守護(hù)線(xiàn)程程序會(huì)被無(wú)限掛起。子線(xiàn)程啟動(dòng)后,父線(xiàn)

程也繼續(xù)執(zhí)行下去,當(dāng)父線(xiàn)程執(zhí)行完最后一條語(yǔ)句print "all over %s" %ctime()后,沒(méi)有等待子線(xiàn)程,直接就退出了,同時(shí)子線(xiàn)程也一同結(jié)束。

start()

開(kāi)始線(xiàn)程活動(dòng)。
運(yùn)行結(jié)果:

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

>>> ========================= RESTART ================================
>>>
I was listening to 愛(ài)情買(mǎi)賣(mài). Thu Apr 17 12:51:45 2014 I was at the 阿凡達(dá)! Thu Apr 17 12:51:45 2014  all over Thu Apr 17 12:51:45 2014

 從執(zhí)行結(jié)果來(lái)看,子線(xiàn)程(muisc 、move )和主線(xiàn)程(print "all over %s" %ctime())都是同一時(shí)間啟動(dòng),但由于主線(xiàn)程執(zhí)行完結(jié)束,所以導(dǎo)致子線(xiàn)程也終止。

繼續(xù)調(diào)整程序:

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

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

    t.join()

    print "all over %s" %ctime()

 我們只對(duì)上面的程序加了個(gè)join()方法,用于等待線(xiàn)程終止。join()的作用是,在子線(xiàn)程完成運(yùn)行之前,這個(gè)子線(xiàn)程的父線(xiàn)程將一直被阻塞。

  注意:  join()方法的位置是在for循環(huán)外的,也就是說(shuō)必須等待for循環(huán)里的兩個(gè)進(jìn)程都結(jié)束后,才去執(zhí)行主進(jìn)程。

運(yùn)行結(jié)果:

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

>>> ========================= RESTART ================================
>>>
I was listening to 愛(ài)情買(mǎi)賣(mài). Thu Apr 17 13:04:11 2014  I was at the 阿凡達(dá)! Thu Apr 17 13:04:11 2014

I was listening to 愛(ài)情買(mǎi)賣(mài). Thu Apr 17 13:04:12 2014
I was at the 阿凡達(dá)! Thu Apr 17 13:04:16 2014
all over Thu Apr 17 13:04:21 2014

  從執(zhí)行結(jié)果可看到,music 和move 是同時(shí)啟動(dòng)的。

  開(kāi)始時(shí)間4分11秒,直到調(diào)用主進(jìn)程為4分22秒,總耗時(shí)為10秒。從單線(xiàn)程時(shí)減少了2秒,我們可以把music的sleep()的時(shí)間調(diào)整為4秒。

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

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

執(zhí)行結(jié)果:

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

>>> ====================== RESTART ================================
>>>
I was listening to 愛(ài)情買(mǎi)賣(mài). Thu Apr 17 13:11:27 2014I was at the 阿凡達(dá)! Thu Apr 17 13:11:27 2014

I was listening to 愛(ài)情買(mǎi)賣(mài). Thu Apr 17 13:11:31 2014
I was at the 阿凡達(dá)! Thu Apr 17 13:11:32 2014
all over Thu Apr 17 13:11:37 2014

子線(xiàn)程啟動(dòng)11分27秒,主線(xiàn)程運(yùn)行11分37秒。

雖然music每首歌曲從1秒延長(zhǎng)到了4 ,但通多程線(xiàn)的方式運(yùn)行腳本,總的時(shí)間沒(méi)變化。

本文從感性上讓你快速理解python多線(xiàn)程的使用,更詳細(xì)的使用請(qǐng)參考其它文檔或資料。

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

class threading.Thread()說(shuō)明:


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

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.


之前講了多線(xiàn)程的一篇博客,感覺(jué)講的意猶未盡,其實(shí),多線(xiàn)程非常有意思。因?yàn)槲覀冊(cè)谑褂秒娔X的過(guò)程中無(wú)時(shí)無(wú)刻都在多進(jìn)程和多線(xiàn)程。我們可以接著之前的例子繼續(xù)講。請(qǐng)先看我的上一篇博客。


 從上面例子中發(fā)現(xiàn)線(xiàn)程的創(chuàng)建是頗為麻煩的,每創(chuàng)建一個(gè)線(xiàn)程都需要?jiǎng)?chuàng)建一個(gè)tx(t1、t2、...),如果創(chuàng)建的線(xiàn)程多時(shí)候這樣極其不方便。下面對(duì)通過(guò)例子進(jìn)行繼續(xù)改進(jìn):

player.py

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

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

def muisc(func):
    for i in range(2):
        print 'Start playing: %s! %s' %(func,ctime())
        sleep(2)

def move(func):
    for i in range(2):
        print 'Start playing: %s! %s' %(func,ctime())
        sleep(5)

def player(name):
    r = name.split('.')[1]
    if r == 'mp3':
        muisc(name)
    else:
        if r == 'mp4':
            move(name)
        else:
            print 'error: The format is not recognized!'

list = ['愛(ài)情買(mǎi)賣(mài).mp3','阿凡達(dá).mp4']

threads = []
files = range(len(list))

#創(chuàng)建線(xiàn)程
for i in files:
    t = threading.Thread(target=player,args=(list[i],))
    threads.append(t)

if __name__ == '__main__':
    #啟動(dòng)線(xiàn)程
    for i in files:
        threads[i].start()
 for i in files:
   threads[i].join()

    #主線(xiàn)程
    print 'end:%s' %ctime()

  有趣的是我們又創(chuàng)建了一個(gè)player()函數(shù),這個(gè)函數(shù)用于判斷播放文件的類(lèi)型。如果是mp3格式的,我們將調(diào)用music()函數(shù),如果是mp4格式的我們調(diào)用move()函數(shù)。哪果兩種格式都不是那么只能告訴用戶(hù)你所提供有文件我播放不了。

  然后,我們創(chuàng)建了一個(gè)list的文件列表,注意為文件加上后綴名。然后我們用len(list) 來(lái)計(jì)算list列表有多少個(gè)文件,這是為了幫助我們確定循環(huán)次數(shù)。

  接著我們通過(guò)一個(gè)for循環(huán),把list中的文件添加到線(xiàn)程中數(shù)組threads[]中。接著啟動(dòng)threads[]線(xiàn)程組,最后打印結(jié)束時(shí)間。

     split()可以將一個(gè)字符串拆分成兩部分,然后取其中的一部分。


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

>>> x = 'testing.py'
>>> s = x.split('.')[1]
>>> if s=='py':
    print s

   
py

運(yùn)行結(jié)果:

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

Start playing: 愛(ài)情買(mǎi)賣(mài).mp3! Mon Apr 21 12:48:40 2014
Start playing: 阿凡達(dá).mp4! Mon Apr 21 12:48:40 2014
Start playing: 愛(ài)情買(mǎi)賣(mài).mp3! Mon Apr 21 12:48:42 2014
Start playing: 阿凡達(dá).mp4! Mon Apr 21 12:48:45 2014
end:Mon Apr 21 12:48:50 2014

現(xiàn)在向list數(shù)組中添加一個(gè)文件,程序運(yùn)行時(shí)會(huì)自動(dòng)為其創(chuàng)建一個(gè)線(xiàn)程。


繼續(xù)改進(jìn)例子:

  通過(guò)上面的程序,我們發(fā)現(xiàn)player()用于判斷文件擴(kuò)展名,然后調(diào)用music()和move() ,其實(shí),music()和move()完整工作是相同的,我們?yōu)槭裁床蛔鲆慌_(tái)超級(jí)播放器呢,不管什么文件都可以播放。經(jīng)過(guò)改造,我的超級(jí)播放器誕生了。

super_player.py

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

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

def super_player(file,time):
    for i in range(2):
        print 'Start playing: %s! %s' %(file,ctime())
        sleep(time)

#播放的文件與播放時(shí)長(zhǎng)
list = {'愛(ài)情買(mǎi)賣(mài).mp3':3,'阿凡達(dá).mp4':5,'我和你.mp3':4}

threads = []
files = range(len(list))

#創(chuàng)建線(xiàn)程
for file,time in list.items():
    t = threading.Thread(target=super_player,args=(file,time))
    threads.append(t)

if __name__ == '__main__':
    #啟動(dòng)線(xiàn)程
    for i in files:
        threads[i].start()
  for i in files:
      threads[i].join()

    #主線(xiàn)程
    print 'end:%s' %ctime()

 

  首先創(chuàng)建字典list ,用于定義要播放的文件及時(shí)長(zhǎng)(秒),通過(guò)字典的items()方法來(lái)循環(huán)的取file和time,取到的這兩個(gè)值用于創(chuàng)建線(xiàn)程。

  接著創(chuàng)建super_player()函數(shù),用于接收f(shuō)ile和time,用于確定要播放的文件及時(shí)長(zhǎng)。

  最后是線(xiàn)程啟動(dòng)運(yùn)行。運(yùn)行結(jié)果:

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

Start playing: 愛(ài)情買(mǎi)賣(mài).mp3! Fri Apr 25 09:45:09 2014
Start playing: 我和你.mp3! Fri Apr 25 09:45:09 2014
Start playing: 阿凡達(dá).mp4! Fri Apr 25 09:45:09 2014
Start playing: 愛(ài)情買(mǎi)賣(mài).mp3! Fri Apr 25 09:45:12 2014
Start playing: 我和你.mp3! Fri Apr 25 09:45:13 2014
Start playing: 阿凡達(dá).mp4! Fri Apr 25 09:45:14 2014
end:Fri Apr 25 09:45:19 2014

 

創(chuàng)建自己的多線(xiàn)程類(lèi)

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

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

class MyThread(threading.Thread):

    def __init__(self,func,args,name=''):
        threading.Thread.__init__(self)
        self.name=name
        self.func=func
        self.args=args

    def run(self):
        apply(self.func,self.args)


def super_play(file,time):
    for i in range(2):
        print 'Start playing: %s! %s' %(file,ctime())
        sleep(time)


list = {'愛(ài)情買(mǎi)賣(mài).mp3':3,'阿凡達(dá).mp4':5}

#創(chuàng)建線(xiàn)程
threads = []
files = range(len(list))

for k,v in list.items():
    t = MyThread(super_play,(k,v),super_play.__name__)
    threads.append(t)       

if __name__ == '__main__':
    #啟動(dòng)線(xiàn)程
    for i in files:
        threads[i].start()
  for i in files:
      threads[i].join()

    #主線(xiàn)程
    print 'end:%s' %ctime()

MyThread(threading.Thread)

創(chuàng)建MyThread類(lèi),用于繼承threading.Thread類(lèi)。


__init__()

使用類(lèi)的初始化方法對(duì)func、args、name等參數(shù)進(jìn)行初始化。 

apply()

  apply(func [, args [, kwargs ]]) 函數(shù)用于當(dāng)函數(shù)參數(shù)已經(jīng)存在于一個(gè)元組或字典中時(shí),間接地調(diào)用函數(shù)。args是一個(gè)包含將要提供給函數(shù)的按位置傳遞的參數(shù)的元組。如果省略了args,任何參數(shù)都不會(huì)被傳遞,kwargs是一個(gè)包含關(guān)鍵字參數(shù)的字典。

apply() 用法:

 

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

#不帶參數(shù)的方法
>>> def say():
    print 'say in'

>>> apply(say)
say in

#函數(shù)只帶元組的參數(shù)
>>> def say(a,b):
    print a,b

>>> apply(say,('hello','蟲(chóng)師'))
hello 蟲(chóng)師

#函數(shù)帶關(guān)鍵字參數(shù)
>>> def say(a=1,b=2):
    print a,b

   
>>> def haha(**kw):
    apply(say,(),kw)

   
>>> haha(a='a',b='b')
a b

MyThread(super_play,(k,v),super_play.__name__)

由于MyThread類(lèi)繼承threading.Thread類(lèi),所以,我們可以使用MyThread類(lèi)來(lái)創(chuàng)建線(xiàn)程。

運(yùn)行結(jié)果:

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

Start playing: 愛(ài)情買(mǎi)賣(mài).mp3! Fri Apr 25 10:36:19 2014
Start playing: 阿凡達(dá).mp4! Fri Apr 25 10:36:19 2014
Start playing: 愛(ài)情買(mǎi)賣(mài).mp3! Fri Apr 25 10:36:22 2014
Start playing: 阿凡達(dá).mp4! Fri Apr 25 10:36:24 2014
all end: Fri Apr 25 10:36:29 2014

相關(guān)文章

最新評(píng)論