python的threading的使用(join方法,多线程,锁threading.Lock和threading.Condition
一、开启多线程方法一
import threading,time def write1():
for i in range(1,5):
print('1')
time.sleep(1) def write12():
for i in range(1, 5):
print('2')
time.sleep(1) # 给两个函数开一个线程,target后面赋值函数名
t1 = threading.Thread(target=write1)
t2 = threading.Thread(target=write12)
# 使用start函数启动这个线程
t1.start()
t2.start()
# 输出线程数量
print(threading.enumerate())
'''
输出:
1
2
[<_MainThread(MainThread, started 21440)>, <Thread(Thread-1, started 2344)>, <Thread(Thread-2, started 3016)>]
1
2
2
1
2
1
'''
二、开启多线程方法二
import threading,time class Write1Threaq(threading.Thread):
def run(self):
for i in range(1, 5):
print('1----%s',threading.current_thread())
time.sleep(1) class Write2Threaq(threading.Thread):
def run(self):
for i in range(1, 5):
# 输出当前线程的名称
print('2----%s',threading.current_thread())
time.sleep(1) def main():
# 继承自threading.Thread之后,只需要实现run方法,执行start函数后,会自动执行run函数
t1 = Write1Threaq()
t2 = Write2Threaq()
t1.start()
t2.start() if __name__ == '__main__':
main() '''
输出:
1----%s <Write1Threaq(Thread-1, started 6304)>
2----%s <Write2Threaq(Thread-2, started 10524)>
2----%s <Write2Threaq(Thread-2, started 10524)>
1----%s <Write1Threaq(Thread-1, started 6304)>
2----%s <Write2Threaq(Thread-2, started 10524)>
1----%s <Write1Threaq(Thread-1, started 6304)>
1----%s2----%s <Write2Threaq(Thread-2, started 10524)>
<Write1Threaq(Thread-1, started 6304)> '''
三、不上锁会出现的错误情况
import time,threading value = 0
def write():
global value
for i in range(1,1000000):
value+=1
print(value) def main():
for i in range(1,3):
t1 = threading.Thread(target=write)
t1.start() if __name__ == '__main__':
main() '''
输出:
# 输出结果显然不符合我们的预期,例如value等于10的时候,两个线程同时进行了两次value+1,但是这个时候两个value+1的
# value都是10,那么结果value就是11,可以说少加了一次1
1143699
1227119
'''
四、使用Lock进行上锁解决三的问题
import time,threading
# 使用多线程锁
glock = threading.Lock() value = 0
def write():
global value
# 上锁
glock.acquire()
for i in range(1,1000000):
value+=1
# 解锁
glock.release()
print(value) def main():
for i in range(1,3):
t1 = threading.Thread(target=write)
t1.start() if __name__ == '__main__':
main() '''
输出:这样就没问题了
999999
1999998
'''
五、condition的使用
threading.Condition 是一个继承自threading.Lock的一个类,所以它也有上锁解锁的功能,上锁acquire,解锁ralease
同时它还具有wait()函数,其功能为将程序在此处阻塞,可以通过函数notify,或者notify_all来进行唤醒,这两个函数要在release之前调用
notify执行一次只会唤醒一个线程,默认是第一个等待的线程
notify_all执行一次会唤醒所有的线程,
import random,threading,time
gcondition = threading.Condition()
Money = 0
def Create():
global Money
num=0
while 1:
if num>10:
break
gcondition.acquire()
Money += random.randint(1,100)
print('生产Money:',Money)
gcondition.notify_all()
gcondition.release()
num+=1
time.sleep(1)
def Consumer(money):
global Money
num = 0
while 1:
if num>2:
break
gcondition.acquire()
while Money<money:
print('金额不足!')
time.sleep(1)
gcondition.wait()
Money-=money
print('总钱数:%s,花费:%s,剩余:%s。',Money+money,money,Money)
gcondition.release()
num+=1
time.sleep(1)
def main():
for i in range(1, 3):
t1 = threading.Thread(target=Create)
t1.start()
print('---------------------')
for i in range(1, 3):
ans = random.randint(1, 100)
t1 = threading.Thread(target=Consumer,args=[ans])# 注意这里的参数传递方式
t1.start()
if __name__ == '__main__':
main()
'''
生产Money: 18
生产Money: 90
---------------------
总钱数:%s,花费:%s,剩余:%s。 90 84 6
金额不足!
生产Money: 75
生产Money: 105
总钱数:%s,花费:%s,剩余:%s。 105 84 21
金额不足!
生产Money: 107
生产Money: 171
总钱数:%s,花费:%s,剩余:%s。 171 84 87
总钱数:%s,花费:%s,剩余:%s。 87 26 61
生产Money: 146
总钱数:%s,花费:%s,剩余:%s。 146 26 120
生产Money: 181
生产Money: 230
生产Money: 253
总钱数:%s,花费:%s,剩余:%s。 253 26 227
生产Money: 275
生产Money: 320
生产Money: 350
生产Money: 423
生产Money: 431
生产Money: 484
生产Money: 527
生产Money: 596
生产Money: 646
生产Money: 721
生产Money: 788
生产Money: 850
'''
Thread参数传递的问题
错误的参数传递
threading.Thread(target=Consumer(ans))
import random,threading,time gcondition = threading.Condition()
Money = 100
def Create():
global Money
num=0
while 1:
if num>10:
break
gcondition.acquire()
Money += random.randint(1,100)
print('生产Money:',Money)
gcondition.notify_all()
gcondition.release()
num+=1
time.sleep(1) def Consumer(money):
global Money
num = 0
while 1:
if num>2:
break
gcondition.acquire()
while Money<money:
print('金额不足!')
time.sleep(1)
gcondition.wait()
Money-=money
print('总钱数:%s,花费:%s,剩余:%s。',Money+money,money,Money)
gcondition.release()
num+=1
time.sleep(1) def main():
for i in range(1, 3):
print('222')
ans = random.randint(1, 100)
t1 = threading.Thread(target=Consumer(ans))
t1.start()
print('---------------------') for i in range(1, 3):
t1 = threading.Thread(target=Create)
t1.start() if __name__ == '__main__':
main() '''
222
总钱数:%s,花费:%s,剩余:%s。 100 12 88
总钱数:%s,花费:%s,剩余:%s。 88 12 76
总钱数:%s,花费:%s,剩余:%s。 76 12 64
222
总钱数:%s,花费:%s,剩余:%s。 64 57 7
金额不足!
程序将会一直卡到这里,这是因为你这样的参数传递方式相当于在start函数没有执行,函数就开始了运行
'''
正确的参数传递
threading.Thread(target=Consumer,args=[ans])
import random,threading,time gcondition = threading.Condition()
Money = 100
def Create():
global Money
num=0
while 1:
if num>10:
break
gcondition.acquire()
Money += random.randint(1,100)
print('生产Money:',Money)
gcondition.notify_all()
gcondition.release()
num+=1
time.sleep(1) def Consumer(money):
global Money
num = 0
while 1:
if num>2:
break
gcondition.acquire()
while Money<money:
print('金额不足!')
time.sleep(1)
gcondition.wait()
Money-=money
print('总钱数:%s,花费:%s,剩余:%s。',Money+money,money,Money)
gcondition.release()
num+=1
time.sleep(1) def main():
the_list = []
for i in range(1, 3):
ans = random.randint(1, 100)
# 参数传递要这样,可不能写成Thread(target=Consumer(ans))
t1 = threading.Thread(target=Consumer,args=[ans])
#the_list.append(t1)
t1.start()
for i in the_list:
i.start()
print('---------------------') for i in range(1, 3):
t1 = threading.Thread(target=Create)
t1.start() if __name__ == '__main__':
main() '''
总钱数:%s,花费:%s,剩余:%s。 222
100 10 90
总钱数:%s,花费:%s,剩余:%s。 90 17 ---------------------
73
生产Money: 99
生产Money: 141
生产Money: 162
总钱数:%s,花费:%s,剩余:%s。 162 17 145
总钱数:%s,花费:%s,剩余:%s。 145 10 135
生产Money: 164
生产Money: 228
总钱数:%s,花费:%s,剩余:%s。 228 17 211
总钱数:%s,花费:%s,剩余:%s。 211 10 201
生产Money: 203
生产Money: 249
生产Money: 276
生产Money: 321
生产Money: 342
生产Money: 387
生产Money: 475
生产Money: 501
生产Money: 596
生产Money: 682
生产Money: 731
生产Money: 823
生产Money: 888
生产Money: 975
生产Money: 1025
生产Money: 1056
生产Money: 1129
'''
六、join函数
如果一个线程在执行过程中要调用另外一个线程,并且等到其完成以后才能接着执行,解决方法就是“那么在调用这个线程时可以使用被调用线程的join方法。”
下面先说一下setDeamon()吧:
其实主线程并不会结束setDeamon(True)的线程,而是当主线程执行完毕之后不会再去关注setDeamon(True)的线程。
所以setDeamon(True)的线程的结果是我们无法获取到的,类似于爱咋咋地?不管你输出什么,我都不看,主线程跑
完就结束整个python process。
而setDeamon(False)的线程会一直受到主线程的关注,就算主线程跑完了也会等setDeamon(False)的线程跑完然后
再结束整个python process。
所以说,就算setDeamon(True)的线程在主线程之后跑完,但如果在setDeamon(False)的线程之前跑完的话,也是会
输出结果的,而不是被所谓的主线程结束就杀死setDeamon(False)的线程。
看一下不同setDeamon运行结果:
#coding:utf-8
import threading
import time def action(arg):
for i in range(2):
print('sub thread start!the thread name is:%s ' % threading.currentThread().getName())
print('the arg is:%s ' %arg)
time.sleep(1) for i in range(4):
t =threading.Thread(target=action,args=(i,))
t.setDaemon(True)
t.start() print('main_thread end!') '''
setDaemon(True)的输出:
sub thread start!the thread name is:Thread-1
the arg is:0
sub thread start!the thread name is:Thread-2
the arg is:1
sub thread start!the thread name is:Thread-3
the arg is:2
sub thread start!the thread name is:Thread-4
the arg is:3
main_thread end!
'''
'''
setDaemon(False)的输出:
sub thread start!the thread name is:Thread-1
the arg is:0
sub thread start!the thread name is:Thread-2
the arg is:1
sub thread start!the thread name is:Thread-3
the arg is:2
sub thread start!the thread name is:Thread-4
the arg is:3
main_thread end!
sub thread start!the thread name is:Thread-2 sub thread start!the thread name is:Thread-1
the arg is:0 the arg is:1
sub thread start!the thread name is:Thread-3
the arg is:2
sub thread start!the thread name is:Thread-4
the arg is:3
'''
join使用的程序正确书写方法:
#coding:utf-8
import threading
import time def action(arg):
time.sleep(1)
print('sub thread start!the thread name is:%s ' % threading.currentThread().getName())
print('the arg is:%s ' %arg)
time.sleep(1) #不正确写法,会导致多线程顺序执行,失去了多线程的意义
for i in range(4):
t =threading.Thread(target=action,args=(i,))
t.setDaemon(True)
t.start()
t.join() #正确写法
thread_list = [] #线程存放列表
for i in range(4):
t =threading.Thread(target=action,args=(i,))
t.setDaemon(True)
thread_list.append(t) for t in thread_list:
t.start() for t in thread_list:
t.join()
print('main_thread end!')
python的threading的使用(join方法,多线程,锁threading.Lock和threading.Condition的更多相关文章
- python小知识- webbrowser模块 + join()方法
一.join描述 将序列中的元素以指定的字符连接生成一个新的字符串. 语法 语法: ‘sep’.join(seq) 参数说明: sep:分隔符.可以为空 seq:要连接的元素序列.字符串.元组.字典 ...
- Python多线程锁
[Python之旅]第六篇(四):Python多线程锁 python lock 多线程 多线程使用方法 多线程锁 摘要: 在多线程程序执行过程中,为什么需要给一些线程加锁以及如何加锁,下面就来 ...
- Python学习笔记16:标准库多线程(threading包裹)
Python主要是通过标准库threading包来实现多线程. 今天,互联网时代,所有的server您将收到大量请求. server要利用多线程的方式的优势来处理这些请求,为了改善网络port读写效率 ...
- python线程join方法
转载:http://www.cnblogs.com/cnkai/p/7504980.html Python多线程与多进程中join()方法的效果是相同的. 下面仅以多线程为例: 首先需要明确几个概念: ...
- C#多线程JOIN方法初探
[说明:刚接触多线程时,弄不明白Join()的作用,查阅了三本书,都不明不白.后来经过自己的一番试验,终于弄清了Join()的本质.大家看看我这种写法是否易懂,是否真的写出了Join()的本质,多提宝 ...
- python笔记9-多线程Threading之阻塞(join)和守护线程(setDaemon)
python笔记9-多线程Threading之阻塞(join)和守护线程(setDaemon) 前言 今天小编YOYO请xiaoming和xiaowang吃火锅,吃完火锅的时候会有以下三种场景: - ...
- 【多线程】java多线程 测试例子 详解wait() sleep() notify() start() join()方法 等
java实现多线程,有两种方法: 1>实现多线程,继承Thread,资源不能共享 2>实现多线程 实现Runnable接口,可以实现资源共享 *wait()方法 在哪个线程中调用 则当前 ...
- python thread的join方法解释
python的Thread类中提供了join()方法,使得一个线程可以等待另一个线程执行结束后再继续运行.这个方法还可以设定一个timeout参数,避免无休止的等待.因为两个线程顺序完成,看起来象一个 ...
- python常错: join() 方法
描述 Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串. 语法 join()方法语法: str.join(sequence) 参数 sequence -- 要连接的 ...
随机推荐
- /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh: line 19: mysql: command not found
[root@test ~]# tail -f /tmp/zabbix_agentd.log /var/lib/zabbix/percona/scripts/get_mysql_stats_wrappe ...
- MySQL全面瓦解17:触发器相关
关于触发器 现实开发中我们经常会遇到这种情况,比如添加.删除和修改信息的时候需要记录日志,我们就要在完成常规的数据库逻辑操作之后再去写入日志表,这样变成了两步操作,更复杂了. 又比如删除一个人员信息的 ...
- LeetCode530. 二叉搜索树的最小绝对差
题目 又是常见的BST,要利用BST的性质,即中序遍历是有序递增序列. 法一.中序遍历 1 class Solution { 2 public: 3 vector<int>res; 4 v ...
- Golang应用性能问题排查分析
背景 公司有一个使用golang开发的采集模块,负责调用多个外部系统采集数据:最近做了一次架构上的调整,将采集模块分成api.job两个子模块,并部署到容器中,拆分前部署在虚机上. 现象 部分采集任务 ...
- SAP中的F4帮助
今天在调试标准程序的时候,意外的发现了一个F4帮助的函数,感觉还是挺好用的. F4IF_FIELD_VALUE_REQUEST从函数名就可以看出是给字段添加F4帮助的. F4 help for fie ...
- JavaSE 基础知识(常识概念 + 基础语法)问答总结/面试题 —— 讲给应届生的 Java 开源知识项目
写在最前面 这个项目是从20年末就立好的 flag,经过几年的学习,回过头再去看很多知识点又有新的理解.所以趁着找实习的准备,结合以前的学习储备,创建一个主要针对应届生和初学者的 Java 开源知识项 ...
- Java基础复习3
循环的嵌套 public class demo8 { public static void main(String[] args) { /* 输出######## ...
- python_mmdt:一种基于敏感哈希生成特征向量的python库(一)
概述 python_mmdt是一种基于敏感哈希的特征向量生成工具.核心算法使用C实现,提高程序执行效率.同时使用python进行封装,方便研究人员使用. 本篇幅主要介绍涉及的相关基本内容与使用,相关内 ...
- jQuery 自动生成二维码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http ...
- FGC频繁 GC卡顿
https://mp.weixin.qq.com/s/I1fp89Ib2Na1-vjmjSpsjQ 线上服务的FGC问题排查,看这篇就够了! 原创 骆俊武 IT人的职场进阶 2020-05-10 ...