-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparsing_server.py
56 lines (46 loc) · 1.8 KB
/
parsing_server.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
from sgf_parser import game_info
import os
import traceback
import atexit
from timeit import default_timer as timer
from multiprocessing.connection import Listener
import pandas as pd
from pathlib import Path
MOUNT_DIR, READ_DIR = Path(os.environ["MOUNT_DIR"]), Path(os.environ["READ_DIR"])
def load_and_parse_games(path: str, fast_parse: bool = False):
if not path:
return pd.DataFrame()
container_path = MOUNT_DIR / Path(path).relative_to(READ_DIR)
sgf_paths = game_info.find_sgf_files(container_path)
print(f"Found {len(sgf_paths)} SGF files in {container_path}")
parsed_dicts = game_info.read_and_parse_all_files(
sgf_paths, fast_parse=fast_parse, processes=min(128, len(sgf_paths) // 2)
)
return pd.DataFrame(parsed_dicts)
if __name__ == "__main__":
listener = Listener(("localhost", 6536), authkey=b"secret password")
def exit_handler():
listener.close()
print("Parsing server is terminating")
atexit.register(exit_handler)
print("Parsing server is running")
while True:
conn = listener.accept() # wait for a connection
try:
data_source, fast_parse = conn.recv()
print("Received request: %s" % data_source)
start = timer()
df = load_and_parse_games(data_source, fast_parse=fast_parse)
conn.send((None, df))
end = timer()
print(f"Sent reply with {len(df.index)} rows. Took {end-start} seconds")
except (AssertionError, EOFError) as e:
print("Failed to parse:", e)
print(traceback.format_exc())
conn.send((e, None))
except Exception as e:
print("Unknown error:", e)
print(traceback.format_exc())
conn.send((e, None))
finally:
conn.close()