无法让Watchdog观察者停止/加入(python)

时间:2015-01-07 08:41:21

标签: python selenium download watchdog

我正在使用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

1 个答案:

答案 0 :(得分:1)

修正:

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

可能不是一个“正确”的解决方案,但似乎可以解决这个问题