pythonで並列処理する方法を探すと色々情報が出てくるんですが、
親子でファイルを分割して処理させるようなサンプルがなかったので、
メモをしておこうと思います。
▼環境
Python 3.12.1
ParentJob.py
import concurrent.futures
from ChildrenJob import ChildrenJob
class ParentJob:
def main(self):
res = []
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
def __exe_children_job(tid, val):
o = ChildrenJob()
r = o.main(tid, val)
return r
futures = []
for tid, val in enumerate(["3","2","1"]):
futures.append(executor.submit(__exe_children_job, tid+1, val))
for future in futures:
res.append(future.result())
return res
o = ParentJob()
r = o.main()
print("--- 結果 ---")
print(r)
ChildrenJob.py
from DB import DB
class ChildrenJob(DB):
def __init__(self):
super().__init__()
def main(self, tid, val):
print(f"Thread-{tid} start")
res = []
with self.conn.cursor() as cur:
cur.execute("select * from sample001 where id = %(id)s", {"id":str(val)})
res = cur.fetchall()
print(f"Thread-{tid} end")
return res
DB.py
import psycopg2
class DB:
dsn = "dbname=postgres host=localhost user=postgres password=postgres"
def __init__(self):
self.conn = psycopg2.connect(self.dsn)
def __del__(self):
self.conn.close()
# ParentJob.pyを実行した結果
Thread-1 start
Thread-1 end
Thread-3 start
Thread-3 end
Thread-2 start
Thread-2 end
--- 結果 ---
[
[('3', 'r3-c1', 'r3-c2', 'r3-c3')],
[('2', 'r2-c1', 'r2-c2', 'r2-c3')],
[('1', 'r1-c1', 'r1-c2', 'r1-c3')]
]