一、开启多线程方法一

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的更多相关文章

  1. python小知识- webbrowser模块 + join()方法

    一.join描述 将序列中的元素以指定的字符连接生成一个新的字符串. 语法 语法: ‘sep’.join(seq) 参数说明: sep:分隔符.可以为空 seq:要连接的元素序列.字符串.元组.字典 ...

  2. Python多线程锁

    [Python之旅]第六篇(四):Python多线程锁   python lock 多线程 多线程使用方法 多线程锁 摘要:   在多线程程序执行过程中,为什么需要给一些线程加锁以及如何加锁,下面就来 ...

  3. Python学习笔记16:标准库多线程(threading包裹)

    Python主要是通过标准库threading包来实现多线程. 今天,互联网时代,所有的server您将收到大量请求. server要利用多线程的方式的优势来处理这些请求,为了改善网络port读写效率 ...

  4. python线程join方法

    转载:http://www.cnblogs.com/cnkai/p/7504980.html Python多线程与多进程中join()方法的效果是相同的. 下面仅以多线程为例: 首先需要明确几个概念: ...

  5. C#多线程JOIN方法初探

    [说明:刚接触多线程时,弄不明白Join()的作用,查阅了三本书,都不明不白.后来经过自己的一番试验,终于弄清了Join()的本质.大家看看我这种写法是否易懂,是否真的写出了Join()的本质,多提宝 ...

  6. python笔记9-多线程Threading之阻塞(join)和守护线程(setDaemon)

    python笔记9-多线程Threading之阻塞(join)和守护线程(setDaemon) 前言 今天小编YOYO请xiaoming和xiaowang吃火锅,吃完火锅的时候会有以下三种场景: - ...

  7. 【多线程】java多线程 测试例子 详解wait() sleep() notify() start() join()方法 等

    java实现多线程,有两种方法: 1>实现多线程,继承Thread,资源不能共享 2>实现多线程  实现Runnable接口,可以实现资源共享 *wait()方法 在哪个线程中调用 则当前 ...

  8. python thread的join方法解释

    python的Thread类中提供了join()方法,使得一个线程可以等待另一个线程执行结束后再继续运行.这个方法还可以设定一个timeout参数,避免无休止的等待.因为两个线程顺序完成,看起来象一个 ...

  9. python常错: join() 方法

    描述 Python join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串. 语法 join()方法语法: str.join(sequence) 参数 sequence -- 要连接的 ...

随机推荐

  1. 【MyBatis】MyBatis 连接池和事务控制

    MyBatis 连接池和事务控制 文章源码 MyBaits 连接池 实际开发中都会使用连接池,因为它可以减少获取连接所消耗的时间.具体可查看 MyBatis 数据源配置在 SqlMapConfig.x ...

  2. 【SpringBoot1.x】SpringBoot1.x 日志

    SpringBoot1.x 日志 日志框架 市面上有很多日志框架,一个日志框架一般包括抽象层和实现. SpringBoot,它的底层是 Spring,而 Spring 框架默认是用 JCL(java. ...

  3. 剑指Offer-连续子数组中的最大和

    题目 输入一个整型数组,数组里有正数也有负数.数组中的一个或连续多个整数组成一个子数组.求所有子数组的和的最大值.要求时间复杂度为 O(n). 输入 [1,-2,3,10,-4,7,2,-5] 返回值 ...

  4. maven 的安装与配置详细步骤

    1. 直接搜索maven 2. 进入后点击download界面,这时出现的是当前最新版本, 当然还有以前的版本可供下载 3. 下载解压到你指定的目录后,需要配置一下环境变量. a. 右键此电脑点击属性 ...

  5. Linux tar压缩和解压

    经常会忘记 tar 压缩和解压命令的使用,故记下来. 1. 打包压缩 tar -zcvf pack.tar.gz pack/ #打包压缩为一个.gz格式的压缩包 tar -jcvf pack.tar. ...

  6. 根据业务摸索出的一个selenium代码模版(python)

    前言 总算入行上班几个月了,不得不说业务是真的不消停啊.. 本人工作上经常遇到一种场景:为甲方做自动化接口处理工具,登录需要短信验证码,, 嘛算是摸索出了一套selenium代码模板,主要解决如下痛点 ...

  7. 1.2V升3.3V芯片,大电流,应用MCU供电,3.3V稳压源

    MCU供电一般是2.5V-5V之间等等都有,1.2V需要升到3.3V的升压芯片来稳压输出3.3V给MCU供电. 同时1.2V的输入电压低,说明供电端的能量也是属于低能量的,对于芯片自身供货是也要求高. ...

  8. mysqlG基于TID模式同步报错 (Last_IO_Errno: 1236)

    mysqlG基于TID模式同步报错Last_IO_Errno: 1236 Last_IO_Error: Got fatal error 1236 from master when reading da ...

  9. 1、kubernetes简介

    Kubernetes简介 文档信息 中文官网:https://kubernetes.io/zh 中文社区:https://www.kubernetes.org.cn/ Kubernetes是容器集群管 ...

  10. 计算机网络安全 —— 报文摘要算法 ME5 (三)

    一.报文摘要算法基本概念 使用加密通常可达到报文鉴别的目的,因为伪造的报文解密后一般不能得到可理解的内容.但简单采用这种方法,计算机很难自动识别报文是否被篡改.另外,对于不需要保密而只需要报文鉴别的网 ...