Python run()函数和start()函数的比较和差别介绍
作者:AGUICHINESE 发布时间:2022-07-04 18:38:47
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'
来源:https://blog.csdn.net/AGUICHINESE/article/details/83269747
猜你喜欢
- 可以在Mac OS X 10.2.x(“Jaguar”)和以上版本上Mac OS X使用二进制安装软件包的PKG格式代替二进制分发版来安装M
- 很多年前,我们就可以轻易的从很多国营商场、火车车厢、饭馆旅馆中看到墙上挂的那个小本本-意见薄,作为经营方与顾客沟通的
- 利用python+ffmpeg合并B站视频及格式转换 B站客户端下载的视频一般有两种格式:早期的多为blv格式(由flv格式转换而来,音视频
- def Num2MoneyFormat( change_number ): ""&q
- 本文实例讲述了php通过获取头信息判断图片类型的方法。分享给大家供大家参考。具体实现方法如下:$filename = '617.gi
- Pillow图像降噪处理由于成像设备、传输媒介等因素的影响,图像总会或多或少的存在一些不必要的干扰信息,我们将这些干扰信息统称为&l
- <?php //设置我们将要使用的文件 $srcurl = "http://localhost/index.php"
- 本文实例为大家分享了python感知机实现的具体代码,供大家参考,具体内容如下一、实现例子李航《统计学方法》p29 例2.1正例:x1=(3
- 前言最近学完Python,写了几个爬虫练练手,网上的教程有很多,但是有的已经不能爬了,主要是网站经常改,可是爬虫还是有通用的思路的,即下载数
- 目录urllib库作用Urllib 库下的几种模块的基本使用一、urllib.request模块1.功能2.常用方法参数说明:总结urlli
- 语法: ROW_NUMBER() OVER([ <partition_by_clause>] <order_by_clau
- 我们很容易用numpy()和from_numpy()将Tensor和NumPy中的数组相互转换。但是需要注意的一点是: 这两个函数所产生的T
- 前言Python语言的turtle库是一个直观有趣的图形绘制函数库,是python语言标准库之一。turtle库也叫海龟库,是turtle绘
- 五一在家写的,和大家分享,支持所有浏览器,添加了左侧菜单点击变色效果<!DOCTYPE html PUBLIC "-//W3
- 由于需求没有做好或者客户是外行,不能很好的配合你做好需求,我在使用asp+access的时候非常头疼,遇到数据结构的改动,就必须修改acce
- 前言thinkphp3.1.2 需要使用cli方法运行脚本折腾了一天才搞定3.1.2的版本真的很古老解决增加cli.php入口文件defin
- 实现代理的方式很多种,流行的web服务器也大都有代理的功能,比如http://www.tornadoweb.cn用的就是nginx的代理功能
- 直接看下面例子my_ld = [lambda x:x*i for i in range(3)]my_list = [ld(2) for ld
- 本文实例讲述了python飞机大战pygame游戏背景设计。分享给大家供大家参考,具体如下:目标背景交替滚动的思路确定显示游戏背景01. 背
- 大家都知道搜索引擎比较喜欢H1。在SEO中H1也是很基础也很重要的一步。但有些时候为了界面风格的原因,很多标题性的文字做成了图片。大多数情况