我正在构建一个运行另一个

Python程序的监视程序计时器,如果它无法从任何线程中找到签入,则关闭整个程序.这样,它最终将能够控制所需的通信端口.计时器的代码如下:

from multiprocessing import Process, Queue

from time import sleep

from copy import deepcopy

PATH_TO_FILE = r'.\test_program.py'

WATCHDOG_TIMEOUT = 2

class Watchdog:

def __init__(self, filepath, timeout):

self.filepath = filepath

self.timeout = timeout

self.threadIdQ = Queue()

self.knownThreads = {}

def start(self):

threadIdQ = self.threadIdQ

process = Process(target = self._executeFile)

process.start()

try:

while True:

unaccountedThreads = deepcopy(self.knownThreads)

# Empty queue since last wake. Add new thread IDs to knownThreads, and account for all known thread IDs

# in queue

while not threadIdQ.empty():

threadId = threadIdQ.get()

if threadId in self.knownThreads:

unaccountedThreads.pop(threadId, None)

else:

print('New threadId < {} > discovered'.format(threadId))

self.knownThreads[threadId] = False

# If there is a known thread that is unaccounted for, then it has either hung or crashed.

# Shut everything down.

if len(unaccountedThreads) > 0:

print('The following threads are unaccounted for:\n')

for threadId in unaccountedThreads:

print(threadId)

print('\nShutting down!!!')

break

else:

print('No unaccounted threads...')

sleep(self.timeout)

# Account for any exceptions thrown in the watchdog timer itself

except:

process.terminate()

raise

process.terminate()

def _executeFile(self):

with open(self.filepath, 'r') as f:

exec(f.read(), {'wdQueue' : self.threadIdQ})

if __name__ == '__main__':

wd = Watchdog(PATH_TO_FILE, WATCHDOG_TIMEOUT)

wd.start()

我还有一个小程序来测试看门狗功能

from time import sleep

from threading import Thread

from queue import SimpleQueue

Q_TO_Q_DELAY = 0.013

class QToQ:

def __init__(self, processQueue, threadQueue):

self.processQueue = processQueue

self.threadQueue = threadQueue

Thread(name='queueToQueue', target=self._run).start()

def _run(self):

pQ = self.processQueue

tQ = self.threadQueue

while True:

while not tQ.empty():

sleep(Q_TO_Q_DELAY)

pQ.put(tQ.get())

def fastThread(q):

while True:

print('Fast thread, checking in!')

q.put('fastID')

sleep(0.5)

def slowThread(q):

while True:

print('Slow thread, checking in...')

q.put('slowID')

sleep(1.5)

def hangThread(q):

print('Hanging thread, checked in')

q.put('hangID')

while True:

pass

print('Hello! I am a program that spawns threads!\n\n')

threadQ = SimpleQueue()

Thread(name='fastThread', target=fastThread, args=(threadQ,)).start()

Thread(name='slowThread', target=slowThread, args=(threadQ,)).start()

Thread(name='hangThread', target=hangThread, args=(threadQ,)).start()

QToQ(wdQueue, threadQ)

正如您所看到的,我需要将线程放入queue.Queue,而单独的对象会慢慢将queue.Queue的输出提供给多处理队列.相反,如果我将线程直接放入多处理队列中,或者在put之间没有QToQ对象休眠,则多处理队列将锁定,并且在看门狗端看起来总是为空.

现在,由于多处理队列应该是线程和进程安全的,我只能假设我在实现中搞砸了一些东西.我的解决方案似乎有效,但也觉得hacky足够我觉得我应该解决它.

我正在使用Python 3.7.2,如果重要的话.

python队列是线程安全的吗_python – 为什么我的多进程队列看起来不是线程安全的?...相关推荐

  1. 用python打印出一个人的照片_python爬取图片实例 - 一个人看日出,一个人看夕阳,一个人走路,一个人欢笑 - OSCHINA - 中文开源技术交流社区...

    小李飞刀图片:http://www.verycd.com/entries/507389/images/view/778356 # -*- coding: utf-8 -*- import reques ...

  2. Python之进程+线程+协程(同步对象、信号量、队列)

    文章目录 Event同步对象 semaphore信号量 队列 本篇是关于Python进程方面的内容了,主要是Event同步对象,信号量和队列 Event同步对象 1.概念: 我们可以对一个线程set一 ...

  3. python线程池并发爬虫_python 并发专题(二):python线程以及线程池相关以及实现...

    一 多线程实现 线程模块 - 多线程主要的内容:直接进行多线程操作,线程同步,带队列的多线程: Python3 通过两个标准库 _thread 和 threading 提供对线程的支持. _threa ...

  4. python 进程间同步_python之路29 -- 多进程与进程同步(进程锁、信号量、事件)与进程间的通讯(队列和管道、生产者与消费者模型)与进程池...

    所谓异步是不需要等待被依赖的任务完成,只是通知被依赖的任务要完成什么工作,依赖的任务也立即执行,只要自己完成了整个任务就算完成了.至于被依赖的任务最终是否真正完成,依赖它的任务无法确定,所以它是不可靠 ...

  5. python多线程执行其他模块的文件_python并发编程--进程线程--其他模块-从菜鸟到老鸟(三)...

    concurrent模块 1.concurrent模块的介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 Proc ...

  6. python的线程组怎么写_Python学习——Python线程

    一.线程创建 1 #方法一:将要执行的方法作为参数传给Thread的构造方法 2 importthreading3 importtime4 5 defshow(arg):6 time.sleep(2) ...

  7. python多进程队列中的队列_python 多进程队列数据处理详解

    我就废话不多说了,直接上代码吧! # -*- coding:utf8 -*- import paho.mqtt.client as mqtt from multiprocessing import P ...

  8. python实现队列_Python学习教程:用队列实现栈

    接着上一期跟大家说的用栈实现队列,这期的 Python学习教程 跟大家讲 用队列实现栈 题目: 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() ...

  9. python堆栈与队列_python语言的堆栈与队列类的实现

    基于python语言的数据结构之堆栈与队列的实现 # 堆栈的实现 # -*- coding: utf-8 -*- """ 栈(stack), 是一种容器,可以存入数据元素 ...

最新文章

  1. 服务器系统策略功能,设置服务器系统口令复杂度策略
  2. JSTL 格式化 BigDecimal对象
  3. java.lang.NullPointerException错误分析
  4. 【机卓14蒋海平-U201411018】机电传动控制课程-《自动化技术中的进给电气传动》第一章的1.1节和1.2节——读书笔记整理...
  5. js调用.net后台事件,和后台调用前台等方法总结
  6. linux 中查找文件,并且将目标文件按时间顺序排序
  7. Redis 学习---(4)Redis 数据类型
  8. 在互联网寒冬季节,他竟然是这样进了百度!值得学习 -- 来自最前沿的实战经验!...
  9. rocketmq怎么保证数据不会重复_RocketMQ 分布式事务消息
  10. html简洁api文档模板,完整word版-API文档模板1.0.0.doc
  11. 抓linux肉鸡教程视频,抓肉鸡的教程和软件免费分享(2018一天抓1000只电脑肉鸡视频)...
  12. comsol 学习笔记【基础知识,磁场与结构场耦合为主】
  13. 华硕win10键盘失灵_华硕键盘失灵一键修复的方法_win10华硕快捷键失灵的解决方法...
  14. Java游戏开发规则
  15. android dialog edittext 弹出软件盘,http://gogo.com-官方网站
  16. 调试The Annotated Transformer
  17. Ubuntu科学操作笔记---kalrry
  18. 窗口看门狗和独立看门狗区别
  19. 130.Impala基准测试
  20. 深度学习——day38 读论文:基于深度学习的数据竞争检测方法(DeleRace计算机研究与发展 2022)

热门文章

  1. HTML基础(part8)--HTML5
  2. Numpy基础(part1)--ndarray数组
  3. HTML基础(part5)--常用标签之重文本标签
  4. nagios 使用mysql_Nagios监控MySQL
  5. 关于前端页面测试和抵御 clickjacking attack 的一些方法
  6. Angular 内容投影出现 No provider for TemplateRef found 错误的单步调试
  7. SAP UI5 的前世今生
  8. SAP 电商云 Spartacus UI CI 脚本分析
  9. SAP Commerce Cloud Spartacus UI 的高阶 reducer 设计
  10. Angular router-outlet占位符层级结构的子节点,运行时是如何插入的