This repository was archived by the owner on May 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
134 lines (122 loc) · 3.88 KB
/
Copy pathmain.py
File metadata and controls
134 lines (122 loc) · 3.88 KB
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
import asyncio
import logging
import os
import os.path as path
import sys
import time
from contextlib import asynccontextmanager as AsyncContextManager
from datetime import datetime
from pathlib import Path
from typing import List
from fastlogging import LogInit
from lark import logger as lark_logger
from tqdm.asyncio import tqdm_asyncio
from utils import (
DataBase,
Deid_Pipeline,
PipeConfig,
build_db_link,
check_schema,
)
@AsyncContextManager
async def setup():
working_dir = Path(path.abspath("."))
# configure logger
logfile = f"{datetime.now().strftime('%Y-%m-%d')}.log"
logfile = working_dir / "logs" / logfile
if not logfile.parent.exists():
os.makedirs(logfile.parent)
with open(logfile, "w") as f:
f.write(f"# Log file {logfile}\n")
logger = LogInit("deid_ppl", console=True, colors=True, pathName=str(logfile))
lark_logger.setLevel(logging.WARNING)
# read config
config_path = PipeConfig.find_config_file(working_dir)
config = PipeConfig(config_path, working_dir, logger)
source = {
table: getattr(config, table)["note_column_names"]
for table in config.table_names
}
await check_schema(
logger,
config.db["host"],
int(config.db["port"]),
config.db["user"],
config.db["password"],
config.db["database"],
config.db["charset"],
source,
config.write_back,
config.write_to_col,
config.db["deid_suffix"],
)
# prepare database connection
async with DataBase(
build_db_link(
config.db["host"],
int(config.db["port"]),
config.db["user"],
config.db["password"],
config.db["database"],
config.db["charset"],
),
logger,
len(config.table_names) * 10,
) as db:
logger.info("Setup phase is finished.")
yield config, db, logger
async def main():
start = time.monotonic()
async with setup() as (config, db, logger):
# temp_file_type = config.deid["temp_file_type"]
table_names: List[str] = config.table_names
logger.debug(f"Working with tables: {', '.join(table_names)}")
tasks = []
for i, table_name in enumerate(table_names):
table_conf = getattr(config, table_name)
tasks.append(
asyncio.create_task(
Deid_Pipeline(
db,
table_name,
table_conf["note_column_names"],
config.batch_size,
config.write_to_col,
config.db["deid_suffix"],
i,
config.write_back,
Path(config.deid["temp_dir"]),
config.deid["script"],
config.deid["config"],
config.autoclean,
).run(
config.worker_size,
table_conf["start_row"],
table_conf["end_row"],
),
name=f"table_{table_name}",
)
)
await tqdm_asyncio.gather(
*tasks,
desc="Deid pipeline",
unit="table",
position=len(table_names),
leave=False,
)
# await asyncio.gather(*tasks)
end = time.monotonic()
logger.info("All deid tasks are done, total time: %.2fs", end - start)
logger.join()
if __name__ == "__main__":
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
asyncio.run(main(), debug=False)
sys.exit(0)
# try:
# loop = asyncio.new_event_loop()
# asyncio.set_event_loop(loop)
# asyncio.run(main(), debug=False)
# sys.exit(0)
# except Exception:
# sys.exit(255)