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

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

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

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

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

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

請看實例:(源代碼)

1 使用run()方法啟動線程,它打印的線程名是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”)

運行結(jié)果:

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

2 使用start()方法啟動的線程名是我們定義線程對象時設(shè)置的name="MyThread"的值,如果沒有設(shè)置name參數(shù)值,則會打印系統(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)) # 當前線程名

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

print(“start() test end”)

運行結(jié)果:

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

3 兩個子線程都用run()方法啟動,但卻是先運行t1.run(),運行完之后才按順序運行t2.run(),兩個線程都工作在主線程,沒有啟動新線程,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”)

運行結(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()方法啟動了兩個新的子線程并交替運行,每個子進程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”)

運行結(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

補充知識:python 文件操作常用輪子

path

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

獲取文件名

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

獲取文件當前文件夾目錄

filename = os.path.dirname(filepath)

同時獲取文件夾和文件名

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

split 文件擴展名

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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

  • Python http接口自動化測試框架實現(xiàn)方法示例

    Python http接口自動化測試框架實現(xiàn)方法示例

    這篇文章主要介紹了Python http接口自動化測試框架實現(xiàn)方法,結(jié)合實例形式分析了Python針對http接口測試的相關(guān)實現(xiàn)與使用操作技巧,需要的朋友可以參考下
    2018-12-12
  • PyTorch的安裝與使用示例詳解

    PyTorch的安裝與使用示例詳解

    本文介紹了熱門AI框架PyTorch的conda安裝方案,與簡單的自動微分示例,并順帶講解了一下PyTorch開源Github倉庫中的兩個Issue內(nèi)容,需要的朋友可以參考下
    2024-05-05
  • 詳解Python設(shè)計模式之策略模式

    詳解Python設(shè)計模式之策略模式

    這篇文章主要介紹了Python設(shè)計模式之策略模式的相關(guān)知識,文中講解非常詳細,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • 基于Python繪制三種不同的中國結(jié)

    基于Python繪制三種不同的中國結(jié)

    馬上就要迎來新年了,就繪制了幾個中國結(jié),嘿嘿!本文為大家整理了三個繪制中國結(jié)的方法,文中的示例代碼講解詳細,快跟隨小編一起動手嘗試一下吧
    2023-01-01
  • Python爬蟲實現(xiàn)爬取京東手機頁面的圖片(實例代碼)

    Python爬蟲實現(xiàn)爬取京東手機頁面的圖片(實例代碼)

    下面小編就為大家分享一篇Python爬蟲實現(xiàn)爬取京東手機頁面的圖片實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • Python正則捕獲操作示例

    Python正則捕獲操作示例

    這篇文章主要介紹了Python正則捕獲操作,結(jié)合具體實例形式分析了Python基于正則表達式的分組、捕獲、替換等相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • Pytest?Allure的安裝與應(yīng)用教程詳解

    Pytest?Allure的安裝與應(yīng)用教程詳解

    Allure?是由?Java?語?開發(fā)的?個輕量級,靈活的測試報告?具,這篇文章主要為大家詳細介紹了Allure的安裝與具體應(yīng)用,感興趣的可以了解下
    2024-03-03
  • Python裝飾器實現(xiàn)函數(shù)運行時間的計算

    Python裝飾器實現(xiàn)函數(shù)運行時間的計算

    這篇文章主要為大家詳細介紹了Python函數(shù)運行時間的計算,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • Python 虛擬環(huán)境工作原理解析

    Python 虛擬環(huán)境工作原理解析

    這篇文章主要介紹了Python 虛擬環(huán)境工作原理解析,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Python判斷對象是否為文件對象(file object)的三種方法示例

    Python判斷對象是否為文件對象(file object)的三種方法示例

    這篇文章主要介紹了Python判斷對象是否為文件對象(file object)的三種方法示例,https://www.pythontab.com/html/2018/pythonhexinbiancheng_1015/1362.html
    2019-04-04

最新評論