欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python thread 并發(fā)且順序運(yùn)行示例

 更新時(shí)間:2009年04月09日 12:14:17   作者:  
以上源文件是對python中的線程的一個(gè)簡單應(yīng)用,實(shí)現(xiàn)了對并發(fā)線程的順序運(yùn)行,也許對你會(huì)有小小幫助
復(fù)制代碼 代碼如下:

#-*- coding:utf-8 -*-
import threading
import time
def fun(name, ls_name, front_thread = None):
'''''
線程啟動(dòng)函數(shù)
通過front_thread來使用線程有序的運(yùn)行
'''
time.clock()
time.sleep(2)
# 如果front_thread存在,則在front_thread運(yùn)行完成后,才運(yùn)行當(dāng)前線程
if front_thread != None:
front_thread.join()
ls_name.append(name)
print "thread %s : %s"% (name, time.clock())

if __name__ == '__main__':
ls_result_name = []
ls_thread = []
time.clock()
# 逐一啟動(dòng)1000個(gè)線程
for i in range(0,10):
if len(ls_thread) == 0:
t = threading.Thread(target=fun, args=(i,ls_result_name,None))
else:
t = threading.Thread(target=fun, args=(i,ls_result_name,ls_thread[-1]))
t.start()
ls_thread.append(t)

# 等待所有線程結(jié)束
for t in ls_thread:
t.join()

print 'ls_result_name:', ls_result_name
print "main thread:%s" % time.clock()

運(yùn)行結(jié)果為:
thread 0 : 1.99962006344
thread 1 : 2.00000866032
thread 2 : 2.00059113658
thread 3 : 2.00080345407
thread 4 : 2.00100068584
thread 5 : 2.00119456523
thread 6 : 2.00138593033
thread 7 : 2.00166753037
thread 8 : 2.00211758757
thread 9 : 2.0024776892
ls_result_name: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
main thread:2.003211302
線程更明細(xì)的使用可參考:
http://docs.python.org/library/threading.html
time.clock模塊的更詳細(xì)介紹可參考:
http://blog.csdn.net/kiki113/archive/2009/03/28/4033017.aspx

相關(guān)文章

  • pytorch中常用的損失函數(shù)用法說明

    pytorch中常用的損失函數(shù)用法說明

    這篇文章主要介紹了pytorch中常用的損失函數(shù)用法說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • 如何利用python正確地為圖像添加高斯噪聲

    如何利用python正確地為圖像添加高斯噪聲

    這篇文章主要給大家介紹了關(guān)于如何利用python正確地為圖像添加高斯噪聲的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • python matplotlib imshow熱圖坐標(biāo)替換/映射實(shí)例

    python matplotlib imshow熱圖坐標(biāo)替換/映射實(shí)例

    這篇文章主要介紹了python matplotlib imshow熱圖坐標(biāo)替換/映射實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Python Barbershop實(shí)現(xiàn)照片換發(fā)型功能

    Python Barbershop實(shí)現(xiàn)照片換發(fā)型功能

    這篇文章主要為大家介紹了一個(gè)開源項(xiàng)目(Barbershop),可以將照片中的發(fā)型更換成另一個(gè),文中實(shí)現(xiàn)過程講解詳細(xì),感興趣的可以學(xué)習(xí)一下
    2022-01-01
  • python裝飾器property和setter用法

    python裝飾器property和setter用法

    這篇文章主要介紹了python裝飾器property和setter用法,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-07-07
  • Python獲取基金網(wǎng)站網(wǎng)頁內(nèi)容、使用BeautifulSoup庫分析html操作示例

    Python獲取基金網(wǎng)站網(wǎng)頁內(nèi)容、使用BeautifulSoup庫分析html操作示例

    這篇文章主要介紹了Python獲取基金網(wǎng)站網(wǎng)頁內(nèi)容、使用BeautifulSoup庫分析html操作,結(jié)合實(shí)例形式分析了Python基于urllib包的網(wǎng)頁內(nèi)容獲取,以及使用BeautifulSoup分析html相關(guān)操作技巧,需要的朋友可以參考下
    2019-06-06
  • Python參數(shù)傳遞實(shí)現(xiàn)過程及原理詳解

    Python參數(shù)傳遞實(shí)現(xiàn)過程及原理詳解

    這篇文章主要介紹了Python參數(shù)傳遞實(shí)現(xiàn)過程及原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Python爬蟲之Selenium多窗口切換的實(shí)現(xiàn)

    Python爬蟲之Selenium多窗口切換的實(shí)現(xiàn)

    這篇文章主要介紹了Python爬蟲之Selenium多窗口切換的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • python ssh 執(zhí)行shell命令的示例

    python ssh 執(zhí)行shell命令的示例

    這篇文章主要介紹了python ssh 執(zhí)行shell命令的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-09-09
  • Python 過濾字符串的技巧,map與itertools.imap

    Python 過濾字符串的技巧,map與itertools.imap

    Python中的map函數(shù)非常有用,在字符轉(zhuǎn)換和字符遍歷兩節(jié)都出現(xiàn)過,現(xiàn)在,它又出現(xiàn)了,會(huì)給我們帶來什么樣的驚喜呢?是不是要告訴我們,map是非常棒的,以后要多找它玩呢?
    2008-09-09

最新評論