侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python run()函数和start()函数的比较和差别介绍

Python  /  管理员 发布于 7年前   410

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中使用input()函数获取用户输入值方式
    下一条:
    对python中arange()和linspace()的区别说明
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客