run() 方法并不启动一个新线程,就是在主线程中调用了一个普通函数而已。
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”)
运行结果:
Start Test run() thread name = MainThread thread name = MainThread thread name = MainThread run() test end
2 使用start()方法启动的线程名是我们定义线程对象时设置的name="MyThread"的值,如果没有设置name参数值,则会打印系统分配的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”)
运行结果:
Start Test start() thread name = MyTryThread thread name = MyTryThread thread name = MyTryThread start() test end
3 两个子线程都用run()方法启动,但却是先运行t1.run(),运行完之后才按顺序运行t2.run(),两个线程都工作在主线程,没有启动新线程,thread ID都是一样的,因此,run()方法仅仅是普通函数调用。
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”)
运行结果:
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”)
运行结果:
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
注意: 对于任何需要处理文件名的问题,都应该使用os.path模块而不是字符串操作。两个原因,os.path能够处理移植性问题,如windows,linux。 另一个原因,不要重复造轮子
获取文件名
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()函数和start()函数的比较和差别介绍就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新动态
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]