python 并发编程 多线程 Thread对象的其他属性或方法
介绍
Thread实例对象的方法
# isAlive(): 返回线程是否活动的。
# getName(): 返回线程名。
# setName(): 设置线程名。 threading模块提供的一些方法:
# threading.currentThread(): 返回当前的线程变量。
# threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
# threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。
current_thread 获取当前线程对象
getName() 获取当前线程名字
from threading import Thread
from threading import current_thread
import time
# current_thread 获取当前线程对象名字
# getName() 获取当前线程名字 def task():
print("%s is running" % current_thread().getName())
time.sleep(2)
print("%s is done" % current_thread().getName()) if __name__ == '__main__':
t = Thread(target=task,)
t.start() '''
Thread-1 is running
Thread-1 is done
'''
默认名字是Thread-1
getName() 获取当前线程名字,t就是current_thread() 当前线程的对象
from threading import Thread, current_thread
import time
# current_thread 获取当前线程对象名字 def task():
print("%s is running" % current_thread().getName())
time.sleep(2)
print("%s is done" % current_thread().getName()) if __name__ == '__main__':
t = Thread(target=task,)
t.start() print(t.getName()) # = current_thread().getName() '''
Thread-1 is running
Thread-1
Thread-1 is done
'''
主线程名字默认是MainThread
from threading import Thread, current_thread
import time
# current_thread 获取当前线程对象名字 def task():
print("%s is running" % current_thread().getName())
time.sleep(2)
print("%s is done" % current_thread().getName()) if __name__ == '__main__':
t = Thread(target=task,)
t.start() print("主线程", current_thread().getName()) # 打印主线程名字 '''
Thread-1 is running
主线程 MainThread
Thread-1 is done
'''
改子线程名字
setName()
from threading import Thread, current_thread
import time
# current_thread 获取当前线程对象名字 def task():
print("%s is running" % current_thread().getName())
time.sleep(2)
print("%s is done" % current_thread().getName()) if __name__ == '__main__':
# name 改子线程名字
t = Thread(target=task, name="子线程1")
t.start() # 改子线程名字
t.setName("儿子线程1")
print("主线程", current_thread().getName()) # 打印主线程名字 '''
子线程1 is running
主线程 MainThread
儿子线程1 is done
'''
启动程序瞬间开启子线程
改主线程名字
current_thread.setName()
from threading import Thread, current_thread
import time
# current_thread 获取当前线程对象名字 def task():
print("%s is running" % current_thread().getName())
time.sleep(2)
print("%s is done" % current_thread().getName()) if __name__ == '__main__':
t = Thread(target=task, name="子线程1")
t.start() current_thread().setName("主线程helo")
print("主线程", current_thread().getName()) '''
子线程1 is running
主线程 主线程helo
子线程1 is done
'''
t.isAlive()
查看子进程是否存活
from threading import Thread, current_thread
import time
# current_thread 获取当前线程对象名字 def task():
print("%s is running" % current_thread().getName())
time.sleep(2)
print("%s is done" % current_thread().getName()) if __name__ == '__main__':
t = Thread(target=task, name="子线程1")
t.start() # 判断子线程是否存活
print(t.isAlive())
print("主线程") '''
子线程1 is running
True
主线程
子线程1 is done
'''
主线程等待子线程运行完,主线程再执行 join()
from threading import Thread, current_thread
import time
# current_thread 获取当前线程对象名字 def task():
print("%s is running" % current_thread().getName())
time.sleep(2)
print("%s is done" % current_thread().getName()) if __name__ == '__main__':
t = Thread(target=task, name="子线程1")
t.start()
print(t.isAlive()) t.join()
# 判断子线程是否存活
print("主线程")
print(t.isAlive()) '''
子线程1 is running
True
子线程1 is done
主线程
False
'''
activeCount(): 返回正在运行的线程数量
from threading import Thread, current_thread, active_count
import time
# current_thread 获取当前线程对象名字 def task():
print("%s is running" % current_thread().getName())
time.sleep(2)
print("%s is done" % current_thread().getName()) if __name__ == '__main__':
t = Thread(target=task, name="子线程1")
t.start() t.join()
print("主线程") # 返回正在运行的线程数量
print(active_count()) '''
子线程1 is running
子线程1 is done
主线程
1
只剩下主线程
'''
enumerate() 返回一个包含正在运行的线程的列表list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
from threading import Thread, current_thread, enumerate
import time
# current_thread 获取当前线程对象名字 def task():
print("%s is running" % current_thread().getName())
time.sleep(2)
print("%s is done" % current_thread().getName()) if __name__ == '__main__':
t = Thread(target=task, name="子线程1")
t.start() print("主线程") # 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
print(enumerate()) '''
子线程1 is running
主线程
[<_MainThread(MainThread, started 38668)>, <Thread(子线程1, started 39424)>]
子线程1 is done
'''
python 并发编程 多线程 Thread对象的其他属性或方法的更多相关文章
- python 并发编程 多进程 Process对象的其他属性方法 join 方法
一 Process对象的join方法 在主进程运行过程中如果想并发地执行其他的任务,我们可以开启子进程,此时主进程的任务与子进程的任务分两种情况 情况一: 在主进程的任务与子进程的任务彼此独立的情况下 ...
- python 并发编程 多进程 Process对象的其他属性方法 terminate与is_alive name pid 函数
进程对象的其他方法一: terminate与is_alive is_alive() 立刻查看的子进程结果 是否存活 from multiprocessing import Process impor ...
- python 并发编程 多线程 目录
线程理论 python 并发编程 多线程 开启线程的两种方式 python 并发编程 多线程与多进程的区别 python 并发编程 多线程 Thread对象的其他属性或方法 python 并发编程 多 ...
- 8 并发编程-(线程)-多线程与多进程的区别&Thread对象的其他属性或方法
1.开启速度 在主进程下开启线程比 开启子进程快 # 1 在 主进程下开启线程 from threading import Thread def work(): print('hello') if ...
- 并发编程 - 线程 - 1.开启线程的两种方式/2.进程与线程的区别/3.Thread对象的其他属性或方法/4.守护线程
1.开启线程的两种方式: 进程,线程: 进程只是用来把资源集中到一起(进程只是一个资源单位,或者说资源集合)而线程才是cpu上的执行单位) 1.同一个进程内的多个线程共享该进程内的地址资源 2.创建线 ...
- python并发编程&多线程(二)
前导理论知识见:python并发编程&多线程(一) 一 threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性 官网链 ...
- python并发编程&多线程(一)
本篇理论居多,实际操作见: python并发编程&多线程(二) 一 什么是线程 在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程 线程顾名思义,就是一条流水线工作的过程,一 ...
- 多线程《四》Thread对象的其他属性和方法
Thread对象的其他属性或方法 介绍 Thread实例对象的方法 # isAlive(): 返回线程是否活动的. # getName(): 返回线程名. # setName(): 设置线程名. th ...
- Python并发编程——多线程与协程
Pythpn并发编程--多线程与协程 目录 Pythpn并发编程--多线程与协程 1. 进程与线程 1.1 概念上 1.2 多进程与多线程--同时执行多个任务 2. 并发和并行 3. Python多线 ...
随机推荐
- mysql常用查询命令
转引自:https://www.cnblogs.com/widows/p/7137184.html 常用mysql命令 show variables like 'character_set_clien ...
- Python 文件I/OⅡ
File对象的属性 一个文件被打开后,你有一个file对象,你可以得到有关该文件的各种信息. 以下是和file对象相关的所有属性的列表: 如下实例: 以上实例输出结果: close()方法 File ...
- 2019春Python程序设计练习6(0423--0429)
1-1 定义Python函数时,如果函数中没有return语句,则默认返回空值None. (2分) T F 1-2 在函数内部没有任何声明的情况下直接为某个变量赋值,这个变量一定是函数 ...
- layui 批量上传
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="piclist.aspx.c ...
- JAVA学长
https://www.cnblogs.com/chenmingjun/p/9697371.html
- CF#356 div2 C 猜数字
C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input standar ...
- poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用
jessica's Reading PJroblem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9134 Accep ...
- python 手动拼接json数据
第一步:分别拼接为字符串 第二步:将字符串转化为list 第三歩:将两个list合并为dict 第四步:将dict转换为接送数据 如: import json keys = ['a', 'b', ' ...
- 2002: [Hnoi2010]Bounce 弹飞绵羊(分块)
2002: [Hnoi2010]Bounce 弹飞绵羊 时间限制: 10 Sec 内存限制: 259 MB 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他 ...
- 在spring官网上下载历史版本的spring插件,springsource-tool-suite
目前spring官网(https://spring.io/tools3/sts/all)上可下载的spring插件只有: ECLIPSE ARCHIVE SIZE 4.9.0 springsource ...