python中Event实现线程间同步介绍
作者:hsy12342611 发布时间:2023-08-18 07:07:08
标签:python,Even,t线程间,同步
前言:
Event在python线程间同步是一种常用的方法,本博客以生产者线程和工作者线程为例说明Event在线程间进行10次同步的应用。
import threading
from threading import Event, Thread
import time
import random
from time import sleep
pevent = Event() #默认是没有事件的
pevent.clear()
cevent = Event()
cevent.clear()
runtimes = 10
mutex_lock = threading.Lock()
class ProducerThread (threading.Thread):
def __init__(self, name, runflag):
threading.Thread.__init__(self)
self.name = name
self.runflag = runflag
self.continueflag = Event()
self.continueflag.set()
def run(self):
global runtimes
sleep(1)
print ("开始线程:" + self.name)
while self.continueflag.isSet():
print("wait consumer ...")
if runtimes == 0:
self.continueflag.clear()
break
pevent.wait()
print("come an consumer ...")
mutex_lock.acquire()
runtimes = runtimes - 1
mutex_lock.release()
pevent.clear()
sleep(1)
cevent.set()
print ("退出线程:" + self.name)
self.runflag.set()
class ConsumerThread (threading.Thread):
def __init__(self,name, runflag):
threading.Thread.__init__(self)
self.name = name
self.runflag = runflag
self.continueflag = Event()
self.continueflag.set()
def run(self):
global runtimes
print ("开始线程:" + self.name)
while self.continueflag.isSet():
if 0 == runtimes:
self.continueflag.clear()
pevent.set()
break
print("I want to consum ... ", runtimes)
pevent.set() #通知生产者要消费
cevent.wait()
cevent.clear()
sleep(1)
print ("退出线程:" + self.name)
self.runflag.set()
def test_pthread():
runflag = Event()
pt = ProducerThread("producer", runflag)
ct = ConsumerThread("consumer", runflag)
pt.start()
ct.start()
pt.join()
ct.join()
runflag.wait()
if __name__ == '__main__':
print('===============begin=================')
test_pthread()
print('===============end=================')
运行结果如下:
来源:https://blog.csdn.net/hsy12342611/article/details/124065314
0
投稿
猜你喜欢
- 本文实例讲述了PHP实现的杨辉三角求解算法。分享给大家供大家参考,具体如下:♥ 前言对于 杨辉三角 是什么的问题,请参考百度百科的详细解释:
- 一、流程分析分析发现密码加密,且发送POST请求时header必须携带x-csrftoken,否则是报403。而x-csrftoken是在第
- table估计每个跟web打过交道的人都会经常接触到,跟js结合能做出很多不错的体验。这里打算结合js做一个系列,包括一些操作和效果,虽然现
- 本文实例讲述了PHP实现判断二叉树是否对称的方法。分享给大家供大家参考,具体如下:问题请实现一个函数,用来判断一颗二叉树是不是对称的。注意,
- 不是很难哦,我们现在就可以实现VBScript在服务器端验证,就是下面的函数:<%FUNCTION TestString(S
- 本文实例为大家分享了php微信公众号获取位置信息的具体代码,供大家参考,具体内容如下<?php/** * wechat php tes
- 一些很实用且必用的js小脚本代码:脚本1:进入页面后自动播放音乐或其它声音文件<embed src="音乐地址&q
- #测试网址: http://localhost/blog/testurl.php?id=5 //获取域名或主机地址 echo $_SERVE
- 前言要想学好爬虫,必须把基础打扎实,之前发布了两篇文章,分别是使用XPATH和requests爬取网页,今天的文章是学习Beautiful
- 在默认情况下,大多数浏览器都会将有序列表中的数字序列的与其列表文字内容显示为相同的字体。这篇快速教程将教你如何使用有序列表(ol)和段落(p
- 前期准备1.beat插件安装pip3 install django-celery-beat2.注册APPINSTALLED_APPS = [
- Python实现对变位词的判断,供大家参考,具体内容如下什么是变位词呢?即两个单词都是由相同的字母组成,而各自的字母顺序不同,譬如pytho
- 在日常的工作中,保护数据免受未授权用户的侵犯是系统管理员特别关心的问题。如果你目前用的是MySQL,就可以使用一些方便的功能来保护系统,来大
- Photoshop Express,也就是传说中的web版photoshop,来了。和想象中的web photoshop相比,这个Photo
- 无论使用int还是varchar,对于Status的多选查询都是不易应对的。举例,常规思维下对CustomerStatus的Enum设置如下
- Python-类属性,实例属性,类方法,静态方法,实例方法类属性和实例属性#coding:utf-8class Student(object
- 一、背景平时工作中经常需要使用各种尺寸、格式的图片来做测试,每次从百度或者谷歌找图都非常麻烦,于是就想作为一个程序员怎么能被这个问题影响效率
- 网站设计似乎朝着越来越复杂的方向发展。这部分源于显示器的逐步增大,随着宽屏显示器的增多,更有加剧网站页面复杂程度的趋势。但是我接触网站设计近
- 代码如下:Dim strName, iLoop For Each strName 
- $r = new ReflectionClass($this); Zend_Debug::dump($r->getConstants(