让这个看门狗查看一个文件夹,并使用一个处理程序将所有新创建的文件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:Users50544342DesktopNewfolder3Files"
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
提前感谢您提供的任何帮助或提示。
您应该尝试更改看门狗的缓冲区大小。看这个
尝试使用更大的缓冲区大小:要更改的值