Got this watchdog looking at a folder and using a handler to LPR all newly created files to a specific printer (defined on a command prompt batch). Problem is that when you submit a lot of files the watchdog will only process 8, 9, 10 or 11 of them... What am I doing wrong? I'm pretty sure there's something wrong with my 'print queue' (maybe getting corrupted) or with the Windows processing timeout...
The script is:
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()
The LPR.bat reads:
lpr.exe -S 127.0.0.1 -P Queue %1
Thanks in advance for any help or tips you may provide.
You should try changing the buffer size of watchdog. Look at this.
try to use a bigger buffer size: Value to change