简体   繁体   English

Python-线程化pyinotify输出。 更好地写入文件或字符串

[英]Python - threaded pyinotify output. Better to write to file or to a string

I have a pyinotify watcher running threaded, called as a separate class, at the moment it just prints its discoveries in a terminal window, if I wanted my script to make an action based on those changes am I better to: 我有一个pyinotify watcher运行线程,称为一个单独的类,目前它只是在终端窗口中打印发现,如果我希望我的脚本基于这些更改来执行操作,那么我更好:

A) modify an array with each notification A)修改每个通知的数组

B) write to a file in /tmp and fetch it from my main script? B)写入/ tmp中的文件并从我的主脚本中获取它?

c) give up programming c)放弃编程

thanks for any input, 感谢您的任何投入,

Stewart 斯图尔特

import Queue
changes = Queue.Queue()

and now use changes.put in the thread that discover the changes, changes.get in the thread that is supposed to act on those changes (there are several other useful methods in Queue that you should check -- also note, per the docs, that the module's renamed to queue , all lowercase, in Python 3). 而现在使用changes.put在发现变化的线程, changes.get在应该作用于这些变化的线程(也有其他几个有用的方法队列 ,你应该检查-也注意到,每文档,在Python 3中将模块重命名为queue ,全部为小写)。 Queues are intrinsically thread-safe and therefore often the best way to arrange cooperation among threads in Python. 队列本质上是线程安全的,因此通常是在Python中安排线程之间的协作的最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM