Python看门狗: Why is my subprocess called continuously?

标签 python subprocess watchdog unison

我是 Python 新手,我正在尝试与我的家庭服务器一致地自动同步。我发现了看门狗并且正在尝试使用它,但是每当我运行“touch test.txt”时,该脚本就会不断启动新的一致进程。由于观察者使用从队列继承的类,因此我认为它应该在将第一个事件从队列顶部弹出后阻塞。我在这里忽略了什么吗?

代码:

#!/usr/bin/python
import sys
import subprocess
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class ChangeHandler(FileSystemEventHandler):
   def on_any_event(self, event):
      subprocess.call(["/usr/bin/unison", "-batch", "-silent", "-ui", "text",
         "default"])

if __name__ == "__main__":
   observer = Observer()
   observer.schedule(ChangeHandler(), '/home/philip', True)
   observer.start()
   try:
      while True:
         time.sleep(1)
   except KeyboardInterrupt:
      observer.stop()
   observer.join()

输出:

props    <-?-> props      /  props    <-?-> props      Documents  props    <-?-> props      Downloads  props    <-?-> props      /  props    <-?-> props      Documents  props    <-?-> props      Downloads  props    <-?-> props      /  props    <-?-> props      Documents  props    <-?-> props      Downloads  props    <-?-> props      /  props    <-?-> props      Documents  props    <-?-> props      Downloads  props    <-?-> props      /  props    <-?-> props      Documents  props    <-?-> props      Downloads  props    <-?-> props      /  props    <-?-> props      Documents  props    <-?-> props      Downloads

最佳答案

OP 在评论中充分回答了这个问题:

It took working with pyinotify to finally understand that I'm synchronizing a directory while simultaneously watching that same directory for filesystem events. Of course, i'll get multiple calls unless I suspend the Observer/Notifier when processing an event.

关于Python看门狗: Why is my subprocess called continuously?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12250779/

相关文章:

python - 是否可以使用 SQLAlchemy 在数据库中拥有列表或数组?

python - 将菜单项添加到 Django 管理站点

python - Python中的日期时间当前年份和月份

python - 如何从另一个用户在 linux 中运行命令行

opencv - 如何解决 OS X 中 CUDA 代码的 GPU 看门狗计时器限制

python - Matplotlib:在 hexbin 图中使用最频繁值的 bin 组添加边框

python - 如何在子进程python中使用linux命令

python - 为进度条解析 apt-get install 的输出

python - 如何在python中为labview应用程序实现看门狗

c# - Win32Exception:访问在 System.Diagnostics.ProcessManager.OpenProcess() 中被拒绝