Skip to Content
고성능 파이썬(2판)
book

고성능 파이썬(2판)

by 오현석, 미샤 고렐릭, 이안 오스발트
May 2021
Beginner to intermediate
528 pages
11h 27m
Korean
Hanbit Media, Inc.
Content preview from 고성능 파이썬(2판)
369
9
multiprocessing 모듈
9.7.2
값에 락 걸기
multiprocessing
모듈은 프로세스 간에 파이썬 객체를 공유하는 방법을 몇 가지 제공한다.
원시 파이썬 객체를 낮은 통신 부가비용을 들여서 공유할 수도 있고,
Manager
를 사용해 고수
준 파이썬 객체 (사전, 리스트 등 )를 공유할 수도 있다 (하지만 동기화 비용 때문에 데이터 공유
속도가 상당히 느려진다 ).
여기서 우리는
multiprocessing
.
Value
객체 (
http
://
bit
.
ly
/
value
_
doc
)를 사용해 프로
세스 사이에 정수를 하나 공유할 것이다.
Value
에는 락이 있지만, 여러분이 원하는 락과는 상
당히 다를 것이다. 동시 읽기와 쓰기를 막지만, 원자적인 값 증가를 제공하지는
않는다
. [예제
9
-
41
]은 이를 보여준다. 프로그램이 잘못된 카운터 값으로 끝난다. 이는 앞에서 봤던 동기화
를 사용하지 않는 파일 기반 예제와 비슷하다.
예제
9-41
락을 사용하지 않아서 잘못된 카운터 값이 나옴
$
python ex2_nolock.py
Expecting to see a count of 4000
We have counted to 2340
$
python -m timeit -s "import ex2_nolock" "ex2_nolock.run_workers()"
20 loops, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

이펙티브 타입스크립트

이펙티브 타입스크립트

Dan Vanderkam
엑셀이 편해지는 파이썬

엑셀이 편해지는 파이썬

펠릭스 춤슈타인
핸즈온 비지도 학습

핸즈온 비지도 학습

강재원, 권재철, 안쿠르 A. 파텔

Publisher Resources

ISBN: 9791162244210