[b0026] python 归纳 (十一)_线程_threading.Thread
总结:
- 默认父线程跑完,子线程并不会马上退出,不像 thread.start_threadXXXX
- 父线程跑完了,并没有退出,一直在那里
- 线程启动速度很快,不占多少开销,不到1毫 秒
代码:
# -*- coding: utf-8 -*-
"""
学习 并发
linux下执行
"""
from threading import Thread
from multiprocessing import Process
import time
import os
import psutil
def work():
print('%s,%f: sub begin %d' % (time.strftime('%M:%S',time.localtime(time.time())),time.time(),os.getpid()))
print "%s,%f: sub %d,%d,%d" % (time.strftime('%M:%S',time.localtime(time.time())),time.time(),os.getpid(),os.getppid(), psutil.Process(os.getpid()).num_threads()) time.sleep(5) print "%s,%f: sub %d,%d,%d" % (time.strftime('%M:%S',time.localtime(time.time())),time.time(),os.getpid(),os.getppid(), psutil.Process(os.getpid()).num_threads())
print('%s,%f: sub end %d' % (time.strftime('%M:%S',time.localtime(time.time())),time.time(),os.getpid())) if __name__ == '__main__': print "%s,%f: main begin" % (time.strftime('%M:%S',time.localtime(time.time())),time.time())
print "%s,%f: main %d,%d,%d" % (time.strftime('%M:%S',time.localtime(time.time())),time.time(),os.getpid(),os.getppid(), psutil.Process(os.getpid()).num_threads())
# 开启子线程
t=Thread(target=work)
t.start()
print '%s,%f: main 线程' % (time.strftime('%M:%S',time.localtime(time.time())),time.time())
print "%s,%f: main %d,%d,%d" % (time.strftime('%M:%S',time.localtime(time.time())),time.time(),os.getpid(),os.getppid(), psutil.Process(os.getpid()).num_threads()) # 开启子进程
# t=Process(target=work)
# t.start()
# print "\nmain %d,%d,%d" % (os.getpid(),os.getppid(), psutil.Process(os.getpid()).num_threads())
# print('\nmain 进程') print "%s,%f: main end" % (time.strftime('%M:%S',time.localtime(time.time())),time.time())
输出:
格式说明:
小时 :秒,时间戳, <当前进程ID, [父 进程ID,当前进程线程数量]>
root@ssmaster:~/python# python test_threadprocess1.py
----------------------------------------------------------
03:04,1536397384.349529: main begin
03:04,1536397384.349605: main 3604,2692,1
03:04,1536397384.350838: sub begin 3604
03:04,1536397384.350939: main 线程
03:04,1536397384.351009: sub 3604,2692,2
03:04,1536397384.351310: main 3604,2692,2
03:04,1536397384.352097: main end
03:09,1536397389.357353: sub 3604,2692,2
03:09,1536397389.358372: sub end 3604
----------------------------------------------------------
输出解读:
- 黄色 标记的中间 输出部分 ,是启动 子线程期间,这段 时间非常快,说明启动线程的速度很快
- 红色标记,此时main线程的逻辑 代码跑完了,但是还是显示有2个线程,对比开始,说明main线程并 没有退出
[b0026] python 归纳 (十一)_线程_threading.Thread的更多相关文章
- [b0032] python 归纳 (十七)_线程同步_信号量Semaphore
代码: # -*- coding: utf-8 -*- """ 多线程并发同步 ,使用信号量threading.Semaphore 逻辑: 多个线程,对同一个共享变量 , ...
- [b0024] python 归纳 (十)_线程 _Thread模块
#!/usr/bin/pythonn # -*- coding: UTF-8 -*- """ 学习线程 thread 总结: 1. 主线程退出,所有子线程都退出 2. ...
- python全栈开发 * 线程锁 Thread 模块 其他 * 180730
一,线程Thread模块1.效率更高(相对于进程) import time from multiprocessing import Process from threading import Thre ...
- [b0021] python 归纳 (七)_获得进程和线程信息
# -*- coding: utf-8 -*- """ 获得线程, 进程 ID,NAME 总结: """ import threading ...
- [b0028] python 归纳 (十三)_队列Queue在多线程中使用
# -*- coding: UTF-8 -*- """ 多线程同时读队列 总结: 1. 会阻塞 if self._jobq.qsize() > 0 进入逻辑,此时被 ...
- Python爬虫(十一)_案例:使用正则表达式的爬虫
本章将结合先前所学的爬虫和正则表达式知识,做一个简单的爬虫案例,更多内容请参考:Python学习指南 现在拥有了正则表达式这把神兵利器,我们就可以进行对爬取到的全部网页源代码进行筛选了. 下面我们一起 ...
- [b0022] python 归纳 (八)_多进程_基本使用
# -*- coding: UTF-8 -*- """ 测试进程使用 multiprocessing.Process 使用: 1. 准备一个函数<fun>,子 ...
- [b0018] python 归纳 (四)_运算符重载
# -*- coding: UTF-8 -*- """ 测试运算符重载 加法 总结: python 运算符表达式其实都是调用 类中方法 __xxx__ + <--- ...
- [b0017] python 归纳 (三)_类名当参数传入
# -*- coding: UTF-8 -*- """ 测试传入类名 总结: 似乎python里面的一切东西都可以当参数传入 函数或者方法 ""&qu ...
随机推荐
- Kibana中文汉化支持
Kibana从6.6.0版本开始支持中文 参考:https://github.com/anbai-inc/Kibana_Hanization 汉化方法如下: 以现行最新版本7.2.0为例,测试机器为W ...
- vue-property-decorator使用指南
在Vue中使用TypeScript时,非常好用的一个库,使用装饰器来简化书写. 1.安装npm i -S vue-property-decorator @Prop @PropSync @Provide ...
- 中文版 Apple 官方 Swift 教程《The Swift Programming Language》
简介 欢迎使用 Swift 关于 Swift 版本兼容性 Swift 初见 Swift 版本历史记录 Swift 教程 基础部分 基本运算符 字符串和字符 集合类型 控制流 函数 闭包 枚举 类和结构 ...
- Automatic Tuning of Undo Retention 常见问题 (Doc ID 1579779.1)
Automatic Tuning of Undo Retention Common Issues (Doc ID 1579779.1) APPLIES TO: Oracle Database - En ...
- ifconfig|grep eth0|awk '{print $5}' 命令详解
因需要将linx下获取某个网中的MAC地址,可以使用如下命令获取: ifconfig|grep eth0|awk '{print $5}' ifconfig: 输出linux下所有网口的信息(包括IP ...
- Re-stheasy
https://dn.jarvisoj.com/challengefiles/ctf2.b93676be23733b2fcda3988c1133c1c1 用IDA-32bit 打开,找到main函数 ...
- HashMap默认加载因子为什么选择0.75?(阿里)
Hashtable 初始容量是11 ,扩容 方式为2N+1; HashMap 初始容量是16,扩容方式为2N; 阿里的人突然问我为啥扩容因子是0.75,回来总结了一下: 提高空间利用率和 减少查询成本 ...
- class与class的继承
class Point{ constructor(x,y){ this.x = x; this.y = y; } toString(){ return '(' + this.x + ',' + thi ...
- AcWing 803. 区间合并
网址 https://www.acwing.com/solution/AcWing/content/1590/ 题目描述给定n个区间[l, r]. 合并所有有交集的区间. 输出合并完成后的区间个数. ...
- C++ 数据类型判断 typeid
#include <iostream> // typeid testing //////////////////////////////////////////////////////// ...