python中run函数作用_Python run()函数和start()函数的比较和差别介绍
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’)
ordef 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
判断是否为文件 fileos.path.isfile(‘/etc/passwd’)
判断是否为文件夹 folderos.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("."))
timeimport 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()函数的比较和差别介绍就是小编分享给大家的全部内容了,希望能给大家一个参考。
python中run函数作用_Python run()函数和start()函数的比较和差别介绍相关推荐
- python中count的作用_python中函数COUNT()的功能是什么
python中函数COUNT()的功能是统计字符串里某个字符出现的次数,语法为[str.count("char", start,end)],其中str为要统计的字符,star为索引 ...
- python中注释的作用_Python函数添加注释的好处
前面的教程中,我们已经学习了 Python 中的注释方式,单行注释以#开头,进行多行注释时通常用三个单引号'''将注释部分包裹起来. 在函数的定义中,常利用多行注释给函数写文档,称为函数文档.为什么要 ...
- python中count的作用_python count函数用法详解
在python中可以使用"count()"函数统计字符串里某个字符出现的次数,该函数用于统计次数,其语法是"count(sub, start= 0,end=len(str ...
- python中import的作用_python使用import报错是什么原因
在练习Python中package的相对导入时,即from . import XXX 或者from .. import XXX 有时会遇到这样两个错误:SystemError: Parent modu ...
- python中赋值语句的作用_python中return可以使用赋值语句吗?
在python中,有各种不同类型的语句.一个python程序是由模块构成的;一个模块由一条或多条语句组成;每个语句由不同的表达式组成;表达式可以创建和操作对象.下面来看看python中的语句. 赋值语 ...
- python中闭包的作用_Python闭包及其作用域
Python闭包及其作用域 关于Python作用域的知识在python作用域有相应的笔记,这个笔记是关于Python闭包及其作用域的详细的笔记 如果在一个内部函数里,对一个外部作用域(但不是全局作用域 ...
- python中注释的作用_python注释是什么意思
注释即对程序代码的解释,在写程序时需适当使用注释,以方便自己和他人理解程序各部分的作用.在执行时,它会被Python解释器忽略,因此不会影响程序的执行. Python支持单行注释与多行注释,具体如下所 ...
- python中ord什么意思_python的ord()函数的反面是什么?
订单(c)Given a string of length one, return an integer representing the Unicode code point of the char ...
- python中count的作用_python中内置的.count是什么?
我一直在checkio.com上解决问题,其中一个问题是:"编写一个函数来查找在给定字符串中出现最大次数的字母" 最重要的解决方案是: import string def chec ...
最新文章
- 美国科学院报告《无止境的前沿:科学的未来75年》
- 统一沟通-技巧-12-Lync-CX600-3000-5000-配置-internet
- 安装mysql删除_mysql安装和删除
- 微博客之后有可能是“切客”
- VC系统扫雷游戏外挂源代码程序下载(转帖
- 相机裁剪旋转_怎么旋转视频画面角度
- 树莓派摄像头 C++ OpenCV YoloV3 实现实时目标检测
- 算法高级(43)-过滤垃圾邮件、短信?-朴素贝叶斯算法
- c语言五子棋双链表,五子棋的悔棋函数怎么写啊。。。双击鼠标左键悔棋(菠萝镇楼)...
- 历史上有哪些最凶计算机病毒?
- python中file是什么意思_Python中的file和open用法详解
- 【项目管理/PMP/PMBOK第六版/新考纲】计算题24道带答案解析!
- 解决iPhone发送短信显示红色感叹号:尚未送达
- 公司员工后台管理系统界面设计-Axure9原型设计
- NFT引发的“十大行业变局”
- 什么是淘宝店铺SKU
- ESP32 Arduino TM1638 数码管 按键 LED
- BZOJ_4398_福慧双修BZOJ_2407_探险_分治+dij
- 在百度搜索结果中屏蔽CSDN
- 射影几何----第四调和点的作图法及证明
热门文章
- 微软开发趣史 | 我们之所以叫它 RAID 是因为它能干掉 BUG
- 努力≠上进!那些持续精进的人有多可怕?
- Java 13 新特性全面解读
- 从“谈芯”走向“用芯” RISC-V 未来前景光明?
- Google 对战微软阿里,AIoT 进入“战国时代”!
- 程序员是否应该创造面向 IDE 而非人类的编程语言?
- 人工智能为什么要从本科生抓起?
- RSS 之父炮轰 Google 强推 HTTPS
- 编程语言 Ruby 如何还能再活 25 年?
- 短视频就这样玩丢了底线