-
Notifications
You must be signed in to change notification settings - Fork 0
/
large-file-handler.py
183 lines (147 loc) · 6.26 KB
/
large-file-handler.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
import json
import logging
import multiprocessing
import os
import sys
import time
from datetime import datetime
from decimal import Decimal
from pathlib import Path
from typing import Dict, Optional
logging.basicConfig(
level=logging.DEBUG,
format="[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s",
handlers=[logging.StreamHandler(sys.stdout)],
)
logger = logging.getLogger(__name__)
class LargeFileHandler(object):
def __init__(
self,
input_file_path: Path,
output_file_path: Path,
cores: int,
file_splitted_size: Optional[int] = None,
):
self.input_file_path = input_file_path
self.output_file_path = output_file_path
self.cores = cores
if not file_splitted_size:
file_size = input_file_path.stat().st_size
self.file_splitted_size = file_size // cores
else:
self.file_splitted_size = file_splitted_size
manager = multiprocessing.Manager()
self.queue = manager.Queue()
def process_wrapper(self, chunk_start: int, chunk_size: int) -> None:
"""The wrapper to call process given chunk_start and chunk_size."""
logger.info(f"Worker: {multiprocessing.current_process().name} start!")
try:
counter = 0
with open(self.input_file_path) as f:
f.seek(chunk_start)
lines = f.read(chunk_size).splitlines()
for line in lines:
self.process(line)
counter += 1
logger.info(
f"Worker: {multiprocessing.current_process().name} Processed: {counter} lines (start {chunk_start}, size {chunk_size})"
)
except Exception as e:
logger.error(
f"Exception caught! Worker: {multiprocessing.current_process().name} counter: {counter}, start {chunk_start}, size {chunk_size})"
)
def process(self, line: str) -> None:
"""The method to execute handle method given line. The return of handle would be dumped to queue."""
dict_per_line = json.loads(line)
self.transform_and_validate(dict_per_line)
result = handle(**dict_per_line)
logger.info(f"Processed {dict_per_line['id']}")
self.queue.put(result + "\n")
@staticmethod
def transform_and_validate(dict_per_line: Dict) -> None:
"""Transform (and validate the schema of) the dict input so it can be consumed by handle function."""
# transform the dict_per_line so it can be used as kwargs for function 'handle'
dict_per_line["datetime"] = datetime.strptime(
dict_per_line["datetime"], "%Y-%m-%d %H:%M:%S.%f"
)
dict_per_line["dt"] = dict_per_line["datetime"]
del dict_per_line["datetime"]
dict_per_line["price"] = Decimal(dict_per_line["price"])
dict_per_line["quantity"] = Decimal(dict_per_line["quantity"])
# TODO: validate the dict using some 3rd party lib
def chunkify(self):
"""Calculate and generate chunk_start, chunk_size of the input file."""
file_end = self.input_file_path.stat().st_size
with open(self.input_file_path, "rb") as f:
chunk_end = f.tell() # current file position
while True:
chunk_start = chunk_end
f.seek(
self.file_splitted_size, 1
) # sets the file's current position at the offset
f.readline() # make sure it goes to the newline char, because we need to read whole line, that's why split size is not equal to chunk
chunk_end = f.tell() # chunk_end keeps moving
yield chunk_start, chunk_end - chunk_start
if chunk_end > file_end:
break
def listener(self):
"""listener of the queue, it will then populate the output file."""
f = open(self.output_file_path, "w")
while True:
m = self.queue.get()
if m == "KILL WORKER":
break
f.write(m)
f.flush()
f.close()
def run(self):
"""Main API to handle the file in parallel."""
pool = multiprocessing.Pool(self.cores)
jobs = []
# start the queue listener for output file population
listener = pool.apply_async(aysnc_result_listener, (self,))
# start processer jobs
for chunk_start, chunk_size in self.chunkify():
jobs.append(
pool.apply_async(aysnc_processer, (self, chunk_start, chunk_size))
)
# wait for jobs to finish
for job in jobs:
job.get()
# wrap up
self.queue.put("KILL WORKER")
pool.close() # no new task
pool.join() # main process wait for tasks to be done
def run_single(self) -> None:
"""Main API to handle the file sequentially."""
try:
with open(self.input_file_path) as f_input, open(
self.output_file_path, "w+"
) as f_output:
for line in f_input:
try:
dict_per_line = json.loads(line)
self.transform_and_validate(dict_per_line)
f_output.write(handle(**dict_per_line) + "\n")
except json.JSONDecodeError:
pass # TODO: handle the exception here
except IOError:
pass # TODO: handle the exception here
def handle(
id: str, symbol: str, price: Decimal, quantity: Decimal, type: str, dt: datetime
) -> str:
# dummy logic
time.sleep(0.1)
return f"processed TRADE {id}. TYPE: {type}, SYMBOL: {symbol}, PRICE: {price.quantize(Decimal('.01'))}, QUANTITY: {quantity.quantize(Decimal('.01'))}, TIME: {dt}"
def aysnc_result_listener(handler_obj: LargeFileHandler):
handler_obj.listener()
def aysnc_processer(handler_obj: LargeFileHandler, chunk_start: int, chunk_size: int):
handler_obj.process_wrapper(chunk_start, chunk_size)
if __name__ == "__main__":
handler = LargeFileHandler(
input_file_path=Path("/Users/zhichenli/sample.txt"),
output_file_path=Path("/Users/zhichenli/result.txt"),
cores=8,
)
handler.run()
# handler.run_single() # if running sequentially