最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在Python中通過(guò)threading模塊定義和調(diào)用線程的方法

 更新時(shí)間:2016年07月12日 18:50:57   作者:磁針石  
由于著名的GIL的存在,Python中雖然能創(chuàng)建多條線程,但卻不能同時(shí)執(zhí)行...anyway,這里我們還是來(lái)學(xué)習(xí)一下在Python中通過(guò)threading模塊定義和調(diào)用線程的方法

定義線程

最簡(jiǎn)單的方法:使用target指定線程要執(zhí)行的目標(biāo)函數(shù),再使用start()啟動(dòng)。

語(yǔ)法:

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

group恒為None,保留未來(lái)使用。target為要執(zhí)行的函數(shù)名。name為線程名,默認(rèn)為Thread-N,通常使用默認(rèn)即可。但服務(wù)器端程序線程功能不同時(shí),建議命名。

#!/usr/bin/env python3
# coding=utf-8
import threading

def function(i):
  print ("function called by thread {0}".format(i))
threads = []

for i in range(5):
  t = threading.Thread(target=function , args=(i,))
  threads.append(t)
  t.start()
  t.join()

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

$ ./threading_define.py 
function called by thread 0
function called by thread 1
function called by thread 2
function called by thread 3
function called by thread 4

確定當(dāng)前線程

#!/usr/bin/env python3
# coding=utf-8

import threading
import time

def first_function():
  print (threading.currentThread().getName()+ str(' is Starting \n'))
  time.sleep(3)
  print (threading.currentThread().getName()+ str( ' is Exiting \n'))
  
def second_function():
  print (threading.currentThread().getName()+ str(' is Starting \n'))
  time.sleep(2)
  print (threading.currentThread().getName()+ str( ' is Exiting \n'))
  
def third_function():
  print (threading.currentThread().getName()+\
  str(' is Starting \n'))
  time.sleep(1)
  print (threading.currentThread().getName()+ str( ' is Exiting \n'))
  
if __name__ == "__main__":
  t1 = threading.Thread(name='first_function', target=first_function)
  t2 = threading.Thread(name='second_function', target=second_function)
  t3 = threading.Thread(name='third_function',target=third_function)
  t1.start()
  t2.start()
  t3.start()

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

$ ./threading_name.py 
first_function is Starting 
second_function is Starting 
third_function is Starting 
third_function is Exiting 
second_function is Exiting 
first_function is Exiting

配合logging模塊一起使用:

#!/usr/bin/env python3
# coding=utf-8

import logging
import threading
import time

logging.basicConfig(
  level=logging.DEBUG,
  format='[%(levelname)s] (%(threadName)-10s) %(message)s',
  )
  
def worker():
  logging.debug('Starting')
  time.sleep(2)
  logging.debug('Exiting')
  
def my_service():
  logging.debug('Starting')
  time.sleep(3)
  logging.debug('Exiting')
  
t = threading.Thread(name='my_service', target=my_service)
w = threading.Thread(name='worker', target=worker)
w2 = threading.Thread(target=worker) # use default name
w.start()
w2.start()
t.start()

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

$ ./threading_names_log.py[DEBUG] (worker  ) Starting
[DEBUG] (Thread-1 ) Starting
[DEBUG] (my_service) Starting
[DEBUG] (worker  ) Exiting
[DEBUG] (Thread-1 ) Exiting
[DEBUG] (my_service) Exiting


在子類中使用線程

前面我們的線程都是結(jié)構(gòu)化編程的形式來(lái)創(chuàng)建。通過(guò)集成threading.Thread類也可以創(chuàng)建線程。Thread類首先完成一些基本上初始化,然后調(diào)用它的run()。run()方法會(huì)會(huì)調(diào)用傳遞給構(gòu)造函數(shù)的目標(biāo)函數(shù)。

#!/usr/bin/env python3
# coding=utf-8

import logging
import threading
import time

exitFlag = 0

class myThread (threading.Thread):
  def __init__(self, threadID, name, counter):
    threading.Thread.__init__(self)
    self.threadID = threadID
    self.name = name
    self.counter = counter
    
  def run(self):
    print ("Starting " + self.name)
    print_time(self.name, self.counter, 5)
    print ("Exiting " + self.name)
    
def print_time(threadName, delay, counter):
  while counter:
    if exitFlag:
      thread.exit()
    time.sleep(delay)
    print ("%s: %s" %(threadName, time.ctime(time.time())))
    counter -= 1
    
# Create new threads
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
# Start new Threads
thread1.start()
thread2.start()
print ("Exiting Main Thread")

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

$ ./threading_subclass.py 
Starting Thread-1
Starting Thread-2
Exiting Main Thread
Thread-1: Tue Sep 15 11:03:21 2015
Thread-2: Tue Sep 15 11:03:22 2015
Thread-1: Tue Sep 15 11:03:22 2015
Thread-1: Tue Sep 15 11:03:23 2015
Thread-2: Tue Sep 15 11:03:24 2015
Thread-1: Tue Sep 15 11:03:24 2015
Thread-1: Tue Sep 15 11:03:25 2015
Exiting Thread-1
Thread-2: Tue Sep 15 11:03:26 2015
Thread-2: Tue Sep 15 11:03:28 2015
Thread-2: Tue Sep 15 11:03:30 2015
Exiting Thread-2

相關(guān)文章

  • Python可變參數(shù)用法實(shí)例分析

    Python可變參數(shù)用法實(shí)例分析

    這篇文章主要介紹了Python可變參數(shù)用法,結(jié)合實(shí)例形式分析了Python可變參數(shù)的具體定義、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2017-04-04
  • Python?使用pip在windows命令行中安裝HDF?reader包的操作方法

    Python?使用pip在windows命令行中安裝HDF?reader包的操作方法

    HDF reader包是一個(gè)常用來(lái)將.mat類型數(shù)據(jù)導(dǎo)入到python在這里插入代碼片中使用的包,非常好用,今天介紹一下,如何在命令行中安裝這個(gè)包,需要的朋友可以參考下
    2022-12-12
  • python機(jī)器學(xué)習(xí)理論與實(shí)戰(zhàn)(一)K近鄰法

    python機(jī)器學(xué)習(xí)理論與實(shí)戰(zhàn)(一)K近鄰法

    這篇文章主要為大家詳細(xì)介紹了python機(jī)器學(xué)習(xí)理論與實(shí)戰(zhàn)第一篇,K近鄰法的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python爬蟲(chóng)獲取全網(wǎng)招聘數(shù)據(jù)實(shí)現(xiàn)可視化分析示例詳解

    Python爬蟲(chóng)獲取全網(wǎng)招聘數(shù)據(jù)實(shí)現(xiàn)可視化分析示例詳解

    這篇文章主要介紹了Python爬蟲(chóng)獲取全網(wǎng)招聘數(shù)據(jù)實(shí)現(xiàn)可視化分析示例詳解,實(shí)現(xiàn)采集一下最新的qcwu招聘數(shù)據(jù),本文列舉了部分代碼以及實(shí)現(xiàn)思路,需要的朋友可以參考下
    2023-07-07
  • 詳解Python變量與注釋高級(jí)用法

    詳解Python變量與注釋高級(jí)用法

    變量與注釋是表達(dá)作者思想的基礎(chǔ),他們對(duì)代碼質(zhì)量的貢獻(xiàn)母庸質(zhì)疑,這篇文章主要介紹了Python變量與注釋高級(jí)用法,需要的朋友可以參考下
    2022-08-08
  • 基于spring boot 日志(logback)報(bào)錯(cuò)的解決方式

    基于spring boot 日志(logback)報(bào)錯(cuò)的解決方式

    今天小編就為大家分享一篇基于spring boot 日志(logback)報(bào)錯(cuò)的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • python自帶的http模塊詳解

    python自帶的http模塊詳解

    本文主要是給大家詳細(xì)講解了Python中自帶的http模塊的使用方法和實(shí)例,非常的細(xì)致,有需要的小伙伴可以參考下
    2016-11-11
  • Django CBV類的用法詳解

    Django CBV類的用法詳解

    這篇文章主要介紹了Django CBV類的用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Pycharm+Flask零基礎(chǔ)項(xiàng)目搭建入門的實(shí)現(xiàn)

    Pycharm+Flask零基礎(chǔ)項(xiàng)目搭建入門的實(shí)現(xiàn)

    本文主要介紹了Pycharm+Flask零基礎(chǔ)項(xiàng)目搭建入門的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 利用Python定位Span標(biāo)簽中文字的實(shí)戰(zhàn)指南

    利用Python定位Span標(biāo)簽中文字的實(shí)戰(zhàn)指南

    在網(wǎng)頁(yè)數(shù)據(jù)抓取和信息提取的過(guò)程中,經(jīng)常需要定位并獲取HTML中特定標(biāo)簽的內(nèi)容,其中,<span>標(biāo)簽是一個(gè)常見(jiàn)的內(nèi)聯(lián)元素,用于對(duì)文本進(jìn)行分組或應(yīng)用樣式,本文將詳細(xì)介紹如何使用Python來(lái)定位并提取<span>標(biāo)簽中的文字,需要的朋友可以參考下
    2024-12-12

最新評(píng)論

涿州市| 阿巴嘎旗| 扬州市| 加查县| 汕头市| 富阳市| 瑞丽市| 临城县| 冷水江市| 自贡市| 广东省| 鄯善县| 改则县| 安新县| 河曲县| 九江市| 万载县| 卢氏县| 潮安县| 双城市| 韩城市| 东乡| 蕲春县| 略阳县| 兴文县| 青铜峡市| 东辽县| 手游| 孟连| 都江堰市| 麻城市| 河北区| 苍梧县| 太湖县| 屏东县| 旺苍县| 古交市| 如皋市| 吉隆县| 东阿县| 酉阳|