【问题标题】:Python watchdog not processing all files in Windows?Python 看门狗不处理 Windows 中的所有文件?
【发布时间】:2022-11-25 14:00:19
【问题描述】:

让这个看门狗查看文件夹并使用处理程序将所有新创建的文件 LPR 到特定打印机(在命令提示符批处理中定义)。问题是,当您提交大量文件时,看门狗只会处理其中的 8、9、10 或 11 个…… 我究竟做错了什么?我很确定我的“打印队列”(可能已损坏)或 Windows 处理超时有问题...

脚本是:

import os
import os.path
import subprocess
from subprocess import *
import sys
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class Watcher:
    DIRECTORY_TO_WATCH = r"C:\Users\50544342\Desktop\Newfolder3\Files"

    def __init__(self):
        self.observer = Observer()

    def run(self):
        event_handler = Handler()
        self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
        self.observer.start()
        try:
            while True:
                time.sleep(5)
        except:
            self.observer.stop()
            print("Error")

        self.observer.join()


class Handler(FileSystemEventHandler):

    @staticmethod
    def on_any_event(event):
        if event.is_directory:
            # LPR print from batch on any event.
            p = subprocess.Popen(['LPR.bat', event.src_path], stdout=PIPE, stderr=PIPE)
            output, errors = p.communicate()
            p.wait() # wait for process to terminate

        elif event.event_type == 'created':
            # LPR print from batch when a file is first created.
            p = subprocess.Popen(['LPR.bat', event.src_path], stdout=PIPE, stderr=PIPE)
            output, errors = p.communicate()
            p.wait() # wait for process to terminate


if __name__ == '__main__':
    w = Watcher()
    w.run()

LPR.bat 内容如下:

lpr.exe -S 127.0.0.1 -P 队列 %1

提前感谢您提供的任何帮助或提示。

【问题讨论】:

    标签: python watchdog lpr


    【解决方案1】:

    您应该尝试更改看门狗的缓冲区大小。 Look at this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      相关资源
      最近更新 更多