網站首頁 編程語言 正文
前言:
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
相關推薦
- 2023-06-05 最新python?字符串數組互轉問題_python
- 2022-09-25 C++ sort比較函數的寫法,最全面的總結
- 2022-08-14 Python?Selenium彈窗處理操作指南_python
- 2022-07-27 Python中的sys模塊、random模塊和math模塊_python
- 2022-10-16 QT編寫tcp通信工具(Client篇)_C 語言
- 2022-07-19 在 NgModule 里通過依賴注入的方式注冊服務實例
- 2022-12-06 React超詳細分析useState與useReducer源碼_React
- 2022-06-25 關于Ubuntu?Server?18.04?LTS?安裝Tomcat并配置systemctl管理To
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支