无法让看门狗观察者 stop/join (python)
Trouble getting Watchdog observer to stop/join (python)
我正在使用 watchdog python 包来检测文件是否已下载(我在 selenium 中这样做)。这是我正在使用的代码,它成功检测到文件创建,但我对 self.observer.stop() 的调用似乎不起作用,程序停滞了。有什么想法吗?
class MyEventHandler(FileSystemEventHandler):
def __init__(self, observer):
self.observer = observer
def on_created(self, event):
print "file created"
self.observer.stop()
def check_download(download_folder):
file_location = download_folder
print(download_folder)
observer = Observer()
event_handler = MyEventHandler(observer)
observer.schedule(event_handler, file_location)
observer.start()
observer.join()
return 0
已修复:
class MyEventHandler(FileSystemEventHandler):
def __init__(self, observer):
self.observer = observer
def on_created(self, event):
if not event.src_path.endswith(".pdf"):
global check
check = 1 # signal download complete
def initiate_download(PDF, download_folder):
file_location = download_folder
global check
check = 0
observer = Observer()
event_handler = MyEventHandler(observer)
observer.schedule(event_handler, file_location)
observer.start()
PDF.click()
while check == 0:
time.sleep(1) # wait for download signal
observer.stop()
observer.join()
return 0
可能不是一个“合适的”解决方案,但它似乎可以解决问题。
我正在使用 watchdog python 包来检测文件是否已下载(我在 selenium 中这样做)。这是我正在使用的代码,它成功检测到文件创建,但我对 self.observer.stop() 的调用似乎不起作用,程序停滞了。有什么想法吗?
class MyEventHandler(FileSystemEventHandler):
def __init__(self, observer):
self.observer = observer
def on_created(self, event):
print "file created"
self.observer.stop()
def check_download(download_folder):
file_location = download_folder
print(download_folder)
observer = Observer()
event_handler = MyEventHandler(observer)
observer.schedule(event_handler, file_location)
observer.start()
observer.join()
return 0
已修复:
class MyEventHandler(FileSystemEventHandler):
def __init__(self, observer):
self.observer = observer
def on_created(self, event):
if not event.src_path.endswith(".pdf"):
global check
check = 1 # signal download complete
def initiate_download(PDF, download_folder):
file_location = download_folder
global check
check = 0
observer = Observer()
event_handler = MyEventHandler(observer)
observer.schedule(event_handler, file_location)
observer.start()
PDF.click()
while check == 0:
time.sleep(1) # wait for download signal
observer.stop()
observer.join()
return 0
可能不是一个“合适的”解决方案,但它似乎可以解决问题。