-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapy_watchdog.py
69 lines (53 loc) · 1.61 KB
/
scrapy_watchdog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding:utf-8 -*-
# @Author: Wei Yi
import time
from watchdog.observers import Observer
from threading import Thread
from watchdog.events import FileSystemEventHandler
import os
import win32api
class Moniter(Thread):
def __init__(self):
Thread.__init__(self)
self.st = time.time()
self.is_finish = False
def run(self):
while not self.is_finish:
#time.sleep(5)
self.cur = time.time()
if self.cur - self.st >= 120:
#print("Long time no change!")
os.system("taskkill /F /IM scrapy.exe")
self.st = time.time()
win32api.ShellExecute(0, 'open', 'start_scrapy.bat', '', '', 1)
moniter = Moniter()
def update_time():
global moniter
moniter.st = time.time()
class MyHandler(FileSystemEventHandler):
def on_any_event(self, event):
if event.is_directory:
return None
elif event.event_type == 'created':
# Take any action here when a file is first created.
#print("Created")
update_time()
elif event.event_type == 'modified':
pass
# Taken any action here when a file is modified.
#print("Change")
if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
path = "sbook1"
observer.schedule(event_handler, path=path, recursive=True)
moniter.start()
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
moniter.stop()
observer.join()
moniter.join()