-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsumer.py
executable file
·68 lines (62 loc) · 2.08 KB
/
consumer.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
import logging
from queue import Queue
from time import time
from typing import Any
import pandas as pd
from arguments import Args
from chunks_processing_info import ChunkFilteringInfo, elapsed
from filter import TextFilter
class Consumer:
def __init__(
self,
text_filter: TextFilter,
args: Args,
):
self.text_filter = text_filter
self.args = args
def run(
self,
chunks_queue: Queue[tuple[int, pd.DataFrame]] | Any,
filtering_info_queue: Queue[tuple[int, ChunkFilteringInfo]] | Any,
):
logging.info(
"%s consumer:start filtering chunks using %s.",
elapsed(self.args.starting_time),
self.args.filter_mode.name,
)
# region filtering
while True:
if chunks_queue.empty():
continue
if (tuple := chunks_queue.get()) is None:
chunks_queue.put(None) # type: ignore # to handle multiple consumers case
break
(index, chunk) = tuple
# filtering
start_time = time()
healthy_rows_number, unhealthy_rows_number = self.text_filter.filter(chunk)
end_time = time()
# add the time taken to filter the chunk
filtering_info_queue.put(
(
index,
ChunkFilteringInfo(
filtering_time=round(
end_time - start_time, self.args.rounding_place
),
number_of_healthy=healthy_rows_number,
number_of_unhealthy=unhealthy_rows_number,
),
)
)
logging.info(
"%s Consumer: finish filtering chunk number %s",
elapsed(self.args.starting_time),
index + 1,
)
# endregion
logging.info(
"%s Consumer:finish of filtering chunks using %s.",
elapsed(self.args.starting_time),
self.args.filter_mode.name,
)