-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.py
198 lines (156 loc) · 4.63 KB
/
master.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import sys
from pathlib import Path
from multiprocessing import Process, Queue, SimpleQueue, JoinableQueue, Lock, Semaphore
from utils.pm import ProcessManager
from utils import notify
from utils.db import Db
from utils.worker import worker
from workers.db import db
from workers.cdx import cdx
from workers.loader import loader
from workers.parser import parser
from config.constants import *
from config.commands import *
class State:
def __init__(self):
self.running = True
self.stopping = False
self.blen = {}
def __setitem__(self, key, value):
if not self.running:
raise("Can't change state when not running")
value = int(value)
if value < 0:
raise("Can't set state to a negative value")
self.blen[key] = value
if not value and self.stopping:
self.update()
def __getitem__(self, key):
return self.blen.get(key, 0)
def stop(self):
self.stopping = True
self.update()
def update(self):
if not self.stopping:
return
for v in self.blen.values():
if v:
return
self.running = False
def controller(ctrl, db_queue, cdx_queue, loader_queue, parser_queue, sem):
pending = {}
cache = []
stats = {
'ttl': [0]*MAX_RETRY,
'check': 0,
'commit': 0,
'store': 0,
}
state = State()
def _check(path, url):
key = path
if key not in pending:
stats['check'] += 1
db_queue.put((CHECK, path, url))
else:
_discard(path, url)
def _discard(path, url):
sem.release()
def _load(path, url):
_retry(path, url)
def _retry(path, url):
key = path
ttl = pending.get(key, MAX_RETRY)
ttl -= 1
stats['ttl'][ttl] += 1
if ttl == 0:
state['inloader'] -= 1
del pending[key]
sem.release()
else:
state['inloader'] += 1
pending[key] = ttl
loader_queue.put((path,url))
def _done(path):
key = path
pending.pop(key, None)
state['inloader'] -= 1
sem.release()
def _unlock():
pass
def _run():
# notify('DEBUG', sem.get_value(), len(pending), loader_queue.qsize(), parser_queue.qsize())
cmd, *args = ctrl.get()
# notify('DO', cmd, *[arg[:10] for arg in args])
CMDS[cmd](*args)
return not state.running
def _cdx(resumeKey):
if resumeKey is None:
state.stop()
else:
notify('CDX', resumeKey)
cdx_queue.put(resumeKey)
def _parse(path, text):
state['inparser'] += 1
parser_queue.put((path, text))
def _parser_done():
state['inparser'] -= 1
def _store(path, status, items=()):
notify('STORE', path)
cache.append((path, status, items))
stats['store'] += 1
if len(cache) > CACHE_MAX_SIZE:
_commit()
def _commit():
db_queue.put((COMMIT, cache))
del cache[:]
stats['commit'] += 1
CMDS = {
CDX: _cdx,
CHECK: _check,
DISCARD: _discard,
DONE: _done,
LOAD: _load,
PARSE: _parse,
RETRY: _retry,
STORE: _store,
UNLOCK: _unlock,
}
cdx_queue.put(None)
worker(_run, "controller", stats)
_commit()
def stdin():
for line in sys.stdin:
yield Path(line.rstrip())
def glob():
for path in Path(ROOT_DIR).glob('**/questions/*/*'):
if path.is_file():
yield path
def parse_args():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--stdin", help="Read path from stdin",
dest='reader',
default=glob, action='store_const', const=stdin)
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_args()
loader_queue = Queue()
parser_queue = Queue()
cdx_queue = Queue()
db_queue = Queue()
ctrl = Queue()
sem = Semaphore(QUEUE_LENGTH)
pm = ProcessManager(
Process(target=controller, args=(ctrl, db_queue, cdx_queue, loader_queue, parser_queue, sem)),
Process(target=db, args=(ctrl, db_queue)),
*[Process(target=cdx, args=(ctrl,cdx_queue, sem, URL_PREFIX)) for n in range(CDX_PROCESS_COUNT)],
*[Process(target=loader, args=(ctrl,loader_queue)) for n in range(LOADER_PROCESS_COUNT)],
*[Process(target=parser, args=(ctrl,parser_queue)) for n in range(PARSER_PROCESS_COUNT)],
)
try:
pm.start()
pm[0].join()
finally:
pm.terminate()