Python 中多线程之 _thread
_thread模块是python 中多线程操作的一种模块方式,主要的原理是派生出多线程,然后给线程加锁,当线程结束的 时候取消锁,然后执行主程序
start_new_thred(function,args,kwargs=none) | 派生一个新的线程,使用给定的argvs,和可选的kwargs 来执行function |
allocate_lock() | 分配locktype 锁对象 |
exit() | 给线程退去指令 |
locktype 锁对象的方法 | |
qcquire(wait=none) | 尝试获取锁对象 |
Locked() | 如果获取了锁对象则返回true ,否则false |
release() | 释放锁 |
程序的代码
#!/usr/bin/python
from time import sleep,ctime
import _thread
loops=[4,2] #定义任务的时间长短
def loop(nloop,nsec,lock): # nloop 任务的名称 nesc 任务执行的时间 lock 锁
print ('loop',nloop,'start at:', ctime()) #输出任务的开始的时间
sleep(nsec) # 任务的执行时间
print('loop',nloop, 'done at:',ctime())#输出任务的结束时间
lock.release() #释放任务的锁
def main():
print ('starting at:',ctime()) #开始执行任务的当前时间
locks=[] # 定义一个空的锁列表
nloops=range(len(loops)) #主要作用是为了下面循环区分具体的任务 for i in nloops:
lock=_thread.allocate_lock() #给任务加上锁
lock.acquire()#获取锁对象
locks.append(lock) #把具体的锁对象加到锁列表里面去
for i in nloops:#循环时间长短
_thread.start_new_thread(loop,(i, loops[i],locks[i])) #派生出两个新的线程 并传递给循环,其中loops[i]传递给 nesc ,locks[i] 传递给 lock
for i in nloops:# 循环时间长短
while locks[i].locked():#判断派生的线程有没有锁,,如果有暂停主线程,直到所有的锁都释放了才会执行主线程
pass
print ('all doneat:', ctime()) if __name__ == '__main__': #执行函数
main()
任务执行的结果
关于给任务加锁的说明 (第一个for 循环)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAmgAAAECCAIAAADFCry/AAAXdElEQVR4nO3dQZqjKhSGYZbVDwtiC7UNdtE7YFir6JkrqDXcAXegkQMe1JNoYirf+9SgmxhCjPoLMeLyuuizj+W/Pkz/SCHHoSyTslIul5eGmJ0rf2n5kM+ijhx9jkPOqWqJ+nIr5UPMIVUlQbxKr547TK2tS/T1k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zs0OOtx11I147QAgN/iyKtqmyt95qtqtSuAeuUAAFwav+MEAMCA4AQAwIDgBADAgOAEAMCA4AQAwIDgBADAgOAEAMCA4AQAwIDgBADAgOAEAMCA4AQAwIDgBADAgOAEAMCA4AQAwIDgBADAgOAEAMCA4AQAwIDgBADAgOAEAMCA4AQAwIDgBADAgOAEAMCA4AQAwIDgBADAgOAEAMDg3ODkjz/++OOPv9/3d1ZwAgCAEcEJAIABwQkAgAHBCQCAwf/iXAwP4exP0gAAAABJRU5ErkJggg==" alt="" />
任务执行的结果
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAikAAAD1CAIAAAAakvI0AAAUUUlEQVR4nO3dT4tc15nH8fsy5mUIYcng9xEQmNiTjTdZBIwxiiHBYBTFkD8mi8QbbTwYgvFAFsGZWYRckoWTEUY2wWOMosnCGy0MavoNZBZSl6rvOed3nue59z51q/r7wZiu0+c+5091nV9XdZd6+Py/Xhx+8dPPnjz59tv777x74+0vvnn06NGXX375b+989S/pl7/6zWD2y1/9pijw8L2XXnrv4aWmT14bhtc+abV88trzgvtXPnzvpV37Rd/9tkl3AMCBDefnn//0F8Orf83PHgDAFTWcn5+f/d+D2PMe13856wEAbN9wfn5+dnb2xJ89AADEkD0AgGzx19wAAIh5+rzn41dvD8Obw/DGdz/4huwBAKxr/zW3bx8/fvwN2QMAWBnZAwDIRvYAALKRPQCAbGQPACAb2QMAyKayx/7PtQEA4ED2AACykT0AgGybzZ5xHMdxPOwcYsaatUcxXhKrb2l3De3dH9E/vMllf1cRMa6oU21f6SsE2DSyZ3G7aU8+WGNF9tTxHqzVm61279DeOsaj3Jsc1WuNi2rdFHvVCir7uMDpmJM9Sz1atvaoW29dh8oeb+cFs6fbM3DWt9rD2eO9X8L7E54wcGrInpI46Wae9WSP5VPd50mt9upTjdZVZA9wSN3sGS8TjZNPVVuMdURl/dnqp6oCRYyV9/tXW6qbEBtFXFhdnbGsHiVQrdW52q7vF/sorTux7Cy+rixaeyJGX2Rc4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s5cV3Pz87O7t7c7+N19wAAGuK/f0eAADiyB4AQDayBwCQjewBAGQjewAA2cgeAEA2sgcAkI3sAQBkI3sAANnIHgBANrIHAJCN7AEAZCN7AADZyB4AQDayBwCQjewBAGQjewAA2cgeAEA2sgcAkI3sAQBkI3sAANnIHgBANrIHAJCN7AEAZCN7AADZyB4AQDayBwCQjewBAGQjewAA2cgeAEA2sgcAkO0iez6787NheGN44T//QvYAANa1/7zn09/dePlPPO8BAKzsefZ8fefGvQ95zQ0AsLp3//k0ez565Wd3PuXnPQCABPyuAQAgG9kDAMhG9gAAspE9AIBsZA8AIBvZAwDIRvYAALKRPQCAbGQPACAb2QMAyEb2AACykT0AgGxkDwAgG9kDAMhG9gAAspE9AIBsZA8AIBvZAwDIRvYAALKRPQCAbGQPACAb2QMAyEb2AACykT0AgGxkDwAgG9kDAMhG9gAAspE9AIBsZA8AIBvZAwDIRvYAALKRPQCAbGQPACAb2QMAyEb2AACykT0AgGxkDwAgG9kDAMhG9gAAspE9AIBsZA8AIBvZAwDIRvYAALKRPQCAbGQPACAb2QMAyEb2AACykT0AgGxkDwAgG9kDAMhG9gAAspE9AIBsZA8AIBvZAwDIRvYAALKRPQCAbGQPACAb2QMAyEb2AACykT0AgGxkDwAgG9kDAMhG9gAAspE9AIBsZA8AIBvZAwDIRvYAALKRPQCAZP8P1Q6UMTJKK5kAAAAASUVORK5CYII=" alt="" />
关于程序的思路流程:
导入时间模块
导入 _thread 模块
定义一个任务时间长短列表
执行mian 函数
输出开始执行的时间
定义空的锁列表
第一个循环主要是获取锁对象,给任务加上锁 把具体的锁对象加到锁列表里面去
第二个循环主要的作用是派生出两个新的线程。执行,loop函数,传递参数给 loop 函数
。
判断线程有没有锁。如果有暂定主线程,没有的话 执行下面代码结束输入
Python 中多线程之 _thread的更多相关文章
- 通过编写聊天程序来熟悉python中多线程及socket的用法
1.引言 Python中提供了丰富的开源库,方便开发者快速就搭建好自己所需要的应用程序.本文通过编写基于tcp/ip协议的通信程序来熟悉python中socket以及多线程的使用. 2.python中 ...
- python中多线程相关
基础知识 进程:进程就是一个程序在一个数据集上的一次动态执行过程 数据集:程序执行过程中需要的资源 进程控制块:完成状态保存的单元 线程:线程是寄托在进程之上,为了提高系统的并发性 线程是进程的实体 ...
- Python中多线程与多进程的恩恩怨怨
概念: 并发:当有多个线程在操作时,如果系统只有一个CPU,则它根本不可能真正同时进行一个以上的线程,它只能把CPU运行时间划分成若干个时间段,再将时间 段分配给各个线程执行,在一个时间段的线程代码运 ...
- Python 中多线程共享全局变量的问题
写在前面不得不看的一些P话: Python 中多个线程之间是可以共享全局变量的数据的. 但是,多线程共享全局变量是会出问题的. 假设两个线程 t1 和 t2 都要对全局变量g_num (默认是0)进行 ...
- python中多线程
多线程 什么是多线程 开启线程的两种方式 进程和线程的区别 Thread对象的其他属性和方法 守护线程 死锁现象与递归锁 信号量.Event定时器 线程Queue 进程池和线程池 什么是多线程 在传统 ...
- python中多线程,多进程,多协程概念及编程上的应用
1, 多线程 线程是进程的一个实体,是CPU进行调度的最小单位,他是比进程更小能独立运行的基本单位. 线程基本不拥有系统资源,只占用一点运行中的资源(如程序计数器,一组寄存器和栈),但是它可以与同属于 ...
- python中多线程,多进程,队列笔记(一)
threading简介:If you want your application to make better use of the computational resources of multi- ...
- python中多线程(1)
一多线程的概念介绍 threading模块介绍 threading模块和multiprocessing模块在使用层面,有很大的相似性. 二.开启多线程的两种方式 1.创建线程的开销比创建进程的开销小, ...
- python中多线程与非线程的执行性能对比
此对比说明了一件事: 如果是IO型应用,多线程有优势, 如果是CPU计算型应用,多线程没必要,还有实现锁呢. #!/usr/bin/env python # -*- coding: utf-8 -*- ...
随机推荐
- Pyhton学习——Day59
参考博客: http://www.cnblogs.com/wupeiqi/articles/6144178.html Form 1. 验证 2. 生成HTML(保留上次输入内容) 3. 初始化默认是 ...
- easyUI datagrid表头的合并
图列: js代码 function initConfigTable(param){ $("#mulConfigureTableBox").empty(); $("#mul ...
- bitset优化背包
题目:https://agc020.contest.atcoder.jp/tasks/agc020_c 回忆下一题,是零一背包,主要的做法就是凑出最接近sum/2的价值,然后发现现在的背包的容量是20 ...
- JS中的Set 与去重
-----------------------Set 与去重----------------------- ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值.Se ...
- OO第三单元总结——JML规格设计
• 1.JML语言的理论基础.应用工具链情况 JML(Java Modeling Language)—— java建模语言,是一种行为接口规范语言( behavioral interface spec ...
- 洛谷 P1518 两只塔姆沃斯牛 The Tamworth Two
P1518 两只塔姆沃斯牛 The Tamworth Two 题目背景 题目描述 两只牛逃跑到了森林里.农夫John开始用他的专家技术追捕这两头牛.你的任务是模拟他们的行为(牛和John). 追击在1 ...
- [CSS3] Image Width with sizes (srcset & sizes)
What if the image won't be displayed at the full viewport width? Then you need something more than s ...
- spring RestTemplate 实例(NameValuePair)
第一种: public List<NameValuePair> getThirdsysPermissionRest(String url,ThirdsysFuncpDTO thirdsys ...
- ZOJ Problem Set - 3820 Building Fire Stations 【树的直径 + 操作 】
题目:problemId=5374" target="_blank">ZOJ Problem Set - 3820 Building Fire Stations 题 ...
- Swift 3.0(一)
一:let 和 var let 声明的是一个常量, var 声明的是一个变量 二:简单数据类型 1.自推出数据类型 let implicitDouble = 70.0 //根据初始化时赋值的数据 ...