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

Python run()函數(shù)和start()函數(shù)的比較和差別介紹

 更新時(shí)間:2020年05月03日 18:41:12   作者:AGUICHINESE  
這篇文章主要介紹了Python run()函數(shù)和start()函數(shù)的比較和差別介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

run() 方法并不啟動(dòng)一個(gè)新線程,就是在主線程中調(diào)用了一個(gè)普通函數(shù)而已。

start() 方法是啟動(dòng)一個(gè)子線程,線程名就是自己定義的name。

因此,如果你想啟動(dòng)多線程,就必須使用start()方法。

請(qǐng)看實(shí)例:(源代碼)

1 使用run()方法啟動(dòng)線程,它打印的線程名是MainThread,也就是主線程。

import threading,time

def worker():
count = 1
while True:
if count >= 4:
break
time.sleep(1)
count += 1
print(“thread name = {}”.format(threading.current_thread().name))

print(“Start Test run()”)
t1 = threading.Thread(target=worker, name=“MyTryThread”)
t1.run()

print(“run() test end”)

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

Start Test run()
thread name = MainThread
thread name = MainThread
thread name = MainThread
run() test end

2 使用start()方法啟動(dòng)的線程名是我們定義線程對(duì)象時(shí)設(shè)置的name="MyThread"的值,如果沒有設(shè)置name參數(shù)值,則會(huì)打印系統(tǒng)分配的Thread-1,Thread-2…這樣的名稱。

import threading,time

def worker():
count = 1
while True:
if count >= 4:
break
time.sleep(2)
count += 1
print(“thread name = {}”.format(threading.current_thread().name)) # 當(dāng)前線程名

print(“Start Test start()”)
t = threading.Thread(target=worker, name=“MyTryThread”)
t.start()
t.join()

print(“start() test end”)

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

Start Test start()
thread name = MyTryThread
thread name = MyTryThread
thread name = MyTryThread
start() test end

3 兩個(gè)子線程都用run()方法啟動(dòng),但卻是先運(yùn)行t1.run(),運(yùn)行完之后才按順序運(yùn)行t2.run(),兩個(gè)線程都工作在主線程,沒有啟動(dòng)新線程,thread ID都是一樣的,因此,run()方法僅僅是普通函數(shù)調(diào)用。

import threading,time

def worker():
count = 1
while True:
if count >= 4:
break
time.sleep(2)
count += 1
print(“thread name = {}, thread id = {}”.format(threading.current_thread().name,
threading.current_thread().ident))

print(“Start Test run()”)
t1 = threading.Thread(target=worker, name=“t1”)
t2 = threading.Thread(target=worker, name=‘t2')

t1.run()
t2.run()

print(“run() test end”)

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

Start Test run()
thread name = MainThread, thread id = 3920
thread name = MainThread, thread id = 3920
thread name = MainThread, thread id = 3920
thread name = MainThread, thread id = 3920
thread name = MainThread, thread id = 3920
thread name = MainThread, thread id = 3920
run() test end

4 使用start()方法啟動(dòng)了兩個(gè)新的子線程并交替運(yùn)行,每個(gè)子進(jìn)程ID也不同。

import threading,time

def worker():
count = 1
while True:
if count >= 4:
break
time.sleep(2)
count += 1
print(“thread name = {}, thread id = {}”.format(threading.current_thread().name,
threading.current_thread().ident))

print(“Start Test start()”)
t1 = threading.Thread(target=worker, name=“MyTryThread1”)
t2 = threading.Thread(target=worker, name=“MyTryThread2”)
t1.start()
t2.start()
t1.join()
t2.join()
print(“start() test end”)

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

Start Test start()
thread name = MyTryThread1, thread id = 4628
thread name = MyTryThread2, thread id = 872
thread name = MyTryThread1, thread id = 4628
thread name = MyTryThread2, thread id = 872
thread name = MyTryThread1, thread id = 4628
thread name = MyTryThread2, thread id = 872
start() test end

補(bǔ)充知識(shí):python 文件操作常用輪子

path

注意: 對(duì)于任何需要處理文件名的問題,都應(yīng)該使用os.path模塊而不是字符串操作。兩個(gè)原因,os.path能夠處理移植性問題,如windows,linux。 另一個(gè)原因,不要重復(fù)造輪子

獲取文件名

import os
filename = os.path.basename(filepath)
print(filename)

獲取文件當(dāng)前文件夾目錄

filename = os.path.dirname(filepath)

同時(shí)獲取文件夾和文件名

dirname, filename = os.path.split(filepath)

split 文件擴(kuò)展名

path_without_ext, ext = os.path.splitext(filepath)
# e.g 'hello/world/read.txt' then
# path_without_ext = hello/world/read, ext = .txt

遍歷文件夾下所有文件方法

import glob

pyfiles = glob.glob('*.py')

or

def getAllFiles(filePath, filelist=[]):
  for root, dirs, files in os.walk(filePath):
    for f in files:
      filelist.append(os.path.join(root, f))
      print(f)
  return filelist

判斷是否為文件 file

os.path.isfile('/etc/passwd')

判斷是否為文件夾 folder

os.path.isdir('/etc/passwd')

是否是軟鏈接

os.path.islink('/usr/local/bin/python3')

軟鏈接真正指向的是

os.path.realpath('/usr/local/bin/python3')

size

獲取文件大小

import os
size = os.path.getsize(filepath)
print(size)

獲取文件夾大小

import os
 
def getFileSize(filePath, size=0):
  for root, dirs, files in os.walk(filePath):
    for f in files:
      size += os.path.getsize(os.path.join(root, f))
      print(f)
  return size
 
print(getFileSize("."))

time

import time
t1 = os.path.gettime('/etc/passwd')
# t1 1272478234.0
t2 = time.ctime(t1)
# t2 'Wed Apr 28 12:10:05 2010'

以上這篇Python run()函數(shù)和start()函數(shù)的比較和差別介紹就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

  • Python 多個(gè)圖同時(shí)在不同窗口顯示的實(shí)現(xiàn)方法

    Python 多個(gè)圖同時(shí)在不同窗口顯示的實(shí)現(xiàn)方法

    今天小編就為大家分享一篇Python 多個(gè)圖同時(shí)在不同窗口顯示的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python語言元素知識(shí)點(diǎn)詳解

    python語言元素知識(shí)點(diǎn)詳解

    在本篇文章中小編給大家分享了關(guān)于python語言元素的相關(guān)知識(shí)點(diǎn)以及實(shí)例代碼,需要的朋友們跟著學(xué)習(xí)下。
    2019-05-05
  • pytorch 如何自定義卷積核權(quán)值參數(shù)

    pytorch 如何自定義卷積核權(quán)值參數(shù)

    這篇文章主要介紹了pytorch 自定義卷積核權(quán)值參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python3批量移動(dòng)指定文件到指定文件夾方法示例

    Python3批量移動(dòng)指定文件到指定文件夾方法示例

    這篇文章主要給大家介紹了關(guān)于Python3批量移動(dòng)指定文件到指定文件夾的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python3具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • matplotlib繪圖實(shí)例演示標(biāo)記路徑

    matplotlib繪圖實(shí)例演示標(biāo)記路徑

    這篇文章主要介紹了matplotlib繪圖實(shí)例演示標(biāo)記路徑,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • Python數(shù)據(jù)類型之String字符串實(shí)例詳解

    Python數(shù)據(jù)類型之String字符串實(shí)例詳解

    這篇文章主要介紹了Python數(shù)據(jù)類型之String字符串,結(jié)合實(shí)例形式詳細(xì)講解了Python字符串的概念、定義、連接、格式化、轉(zhuǎn)換、查找、截取、判斷等常見操作技巧,需要的朋友可以參考下
    2019-05-05
  • python 爬取影視網(wǎng)站下載鏈接

    python 爬取影視網(wǎng)站下載鏈接

    一個(gè)簡(jiǎn)單的爬取影視網(wǎng)站下載鏈接的爬蟲,非常適合新手學(xué)習(xí),感興趣的朋友可以參考下
    2021-05-05
  • python PyTorch預(yù)訓(xùn)練示例

    python PyTorch預(yù)訓(xùn)練示例

    這篇文章主要介紹了python PyTorch預(yù)訓(xùn)練示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02
  • python3使用flask編寫注冊(cè)post接口的方法

    python3使用flask編寫注冊(cè)post接口的方法

    今天小編就為大家分享一篇python3使用flask編寫注冊(cè)post接口的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python?scipy利用快速傅里葉變換實(shí)現(xiàn)濾波

    Python?scipy利用快速傅里葉變換實(shí)現(xiàn)濾波

    這篇文章主要為大家詳細(xì)介紹了Python?scipy如何利用快速傅里葉變換實(shí)現(xiàn)濾波,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01

最新評(píng)論

湟中县| 林西县| 牡丹江市| 龙岩市| 昔阳县| 通城县| 卓尼县| 台北县| 江山市| 古浪县| 休宁县| 开平市| 昭觉县| 马山县| 牟定县| 高碑店市| 柏乡县| 岳普湖县| 邵东县| 金华市| 青冈县| 台东市| 北宁市| 桃园县| 舟曲县| 常德市| 河南省| 石柱| 丰台区| 乌苏市| 吐鲁番市| 洪湖市| 辽阳县| 望谟县| 桦南县| 镇安县| 大丰市| 亚东县| 蕲春县| 大足县| 晋中市|