업무적 수요로 batch성 업무를 처리하는 일은 다반사다. Python에는 제대로 된 thread 가 없어 process로 풀을 구성하여 구현하는 법을 정리한다.
프로세스 pool로 돌리기
import multiprocessing
def put_data(a,b):
#업무 코드
if __name__=="__main__":
#풀에서 가용한 CPU 개수 설정
# pool = mp.pool(mp.cpu_count())
pool = multiprocessing.Pool(processes=6)
step = 10000
idx =100000
forList =[]
while(idx< 5000000):
forList.append((idx, idx+step))
idx =idx + step
# forList.append()
pool.starmap(putData,forList)
pool.join()
업무 함수 put_data의 입력쌍을 만들어주고 starmap을 이용하여 process pool에 던져 주고 시작 시킨다.
'Python' 카테고리의 다른 글
[python] AWS lambda layer로 필요 라이브러리 추가 (0) | 2022.03.12 |
---|---|
[python] Docker 기반으로 Batch성 업무를 간소화 하자 (0) | 2021.12.22 |
Comment