av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:19日期:2022-07-26 14:49:36

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

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

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

請看實例:(源代碼)

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

import threading,timedef worker():count = 1while True:if count >= 4:breaktime.sleep(1)count += 1print(“thread name = {}”.format(threading.current_thread().name))print(“Start Test run()”)t1 = threading.Thread(target=worker, name=“MyTryThread”)t1.run()print(“run() test end”)

運行結果:

Start Test run()thread name = MainThreadthread name = MainThreadthread name = MainThreadrun() test end

2 使用start()方法啟動的線程名是我們定義線程對象時設置的name='MyThread'的值,如果沒有設置name參數值,則會打印系統分配的Thread-1,Thread-2…這樣的名稱。

import threading,timedef worker():count = 1while True:if count >= 4:breaktime.sleep(2)count += 1print(“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”)

運行結果:

Start Test start()thread name = MyTryThreadthread name = MyTryThreadthread name = MyTryThreadstart() test end

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

import threading,timedef worker():count = 1while True:if count >= 4:breaktime.sleep(2)count += 1print(“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”)

運行結果:

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

4 使用start()方法啟動了兩個新的子線程并交替運行,每個子進程ID也不同。

import threading,timedef worker():count = 1while True:if count >= 4:breaktime.sleep(2)count += 1print(“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”)

運行結果:

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

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

path

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

獲取文件名

import osfilename = 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 ossize = 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 timet1 = os.path.gettime(’/etc/passwd’)# t1 1272478234.0t2 = time.ctime(t1)# t2 ’Wed Apr 28 12:10:05 2010’

以上這篇Python run()函數和start()函數的比較和差別介紹就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 成年网站在线观看 | 一区二区色 | 在线免费观看黄色片 | 久久亚洲国产精品 | 日韩欧美在线观看视频 | 男人爱看的网站 | 欧美日韩精品在线 | 偷拍一区二区三区 | 神马影院午夜伦理片 | 玖草在线| 婷婷色婷婷 | 伊人av网 | 在线观看黄色片 | аⅴ资源新版在线天堂 | 久热精品视频在线观看 | 97成人在线 | 激情婷婷综合 | 三级黄色 | 成人在线观看免费爱爱 | 日韩精品第一页 | 亚洲69视频 | 成人三级晚上看 | 美日韩一区二区三区 | 免费看黄色录像 | 夜夜草av | 亚洲精品免费在线 | 欧美国产日韩精品 | 亚洲精品日韩精品 | 国产精品理论片 | 天堂a在线 | 艳妇诱春(第5部分)(h) | 夜夜嗨av一区二区三区网页 | 午夜精品久久久久久久久久久久 | 天天躁日日躁狠狠躁伊人 | 99精品成人 | 国产视频一区在线观看 | 天天综合视频 | 一区二区高清视频 | 在线免费观看日韩av | 中文字幕专区 | 日韩色网站 |