파이썬[Python]: multiprocessing - Process 클래스
파이썬(Phthon): multiprocessing 모듈 - Process 클래스(class)
/// 설명
구분되어진 프로세스를 제어하는 클래스입니다.(2가지 방식: 콜러블 객체를 전달, run() 메서드의 오버라이딩)
참고: 프로세스
group: 사용하지 않습니다.
target: run() 메서드에 의해 시행되는 콜러블 객체입니다.
name: 프로세스에 이름을 부여합니다. 기본값은 Process-N, N은 정수
args: target 에 사용되어지는 튜플입니다.
kwargs: target 에 사용되어지는 딕셔너리입니다.
deamon: daemonic process. (백그라운드에서 시행되는 듯...)
※ 형식
class multiprocessing.Process(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)
reference
https://docs.python.org/3/library/multiprocessing.html#module-multiprocessing
참고: 프로세스
group: 사용하지 않습니다.
target: run() 메서드에 의해 시행되는 콜러블 객체입니다.
name: 프로세스에 이름을 부여합니다. 기본값은 Process-N, N은 정수
args: target 에 사용되어지는 튜플입니다.
kwargs: target 에 사용되어지는 딕셔너리입니다.
deamon: daemonic process. (백그라운드에서 시행되는 듯...)
※ 형식
class multiprocessing.Process(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)
reference
https://docs.python.org/3/library/multiprocessing.html#module-multiprocessing
/// 예제
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 | import multiprocessing import datetime import time def test_multiprocessing(i): print(f' ---> {__name__}: test_multiprocessing {i} is started!') time.sleep(i) print(f' {i}: test_multiprocessing - sleep({i}) ---') print(f' ---> Time: {datetime.datetime.now().time()}') def main(): print('---> Time:', datetime.datetime.now().time()) for i in range(1, 4): m = multiprocessing.Process(target=test_multiprocessing, args=(i,)) m.start() # new process: __name__ = __mp_main__ when using Windows(Pycharm) # Windows does not have fork print('---> Time:', datetime.datetime.now().time()) print('-' * 50) if __name__ == '__main__': # parent process only has __name__ = __main__ when using Windows(Pycharm) multiprocessing.freeze_support() # support Windows executable main() # ---> Time: 13:34:50.828798 # ---> Time: 13:34:50.849745 # -------------------------------------------------- # ---> __mp_main__: test_multiprocessing 1 is started! # ---> __mp_main__: test_multiprocessing 2 is started! # ---> __mp_main__: test_multiprocessing 3 is started! # 1: test_multiprocessing - sleep(1) --- # ---> Time: 13:34:51.921100 # 2: test_multiprocessing - sleep(2) --- # ---> Time: 13:34:52.918703 # 3: test_multiprocessing - sleep(3) --- # ---> Time: 13:34:53.932210 | cs |
/// 예제 name
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 | import multiprocessing import datetime import time def test_multiprocessing(i): print(f' ---> {__name__}: test_multiprocessing {i} is started!') time.sleep(i) print(f' {i}: test_multiprocessing - sleep({i}) ---') print(f' ---> Time: {datetime.datetime.now().time()}') def main(): print('---> Time:', datetime.datetime.now().time()) m = multiprocessing.Process(target=test_multiprocessing, args=(0,)) print('Name:', m.name) m.start() # new process: __name__ = __mp_main__ when using Windows(Pycharm) # Windows does not have fork print('---> Time:', datetime.datetime.now().time()) print('-' * 50) if __name__ == '__main__': # parent process only has __name__ = __main__ when using Windows(Pycharm) multiprocessing.freeze_support() # support Windows executable main() # ---> Time: 14:13:21.437985 # Name: Process-1 # ---> Time: 14:13:21.449951 # -------------------------------------------------- # ---> __mp_main__: test_multiprocessing 0 is started! # 0: test_multiprocessing - sleep(0) --- # ---> Time: 14:13:21.517770 | cs |
/// 예제 daemon
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 | import multiprocessing import datetime import time def test_multiprocessing(i): print(f' ---> {__name__}: test_multiprocessing {i} is started!') time.sleep(i) print(f' {i}: test_multiprocessing - sleep({i}) ---') print(f' ---> Time: {datetime.datetime.now().time()}') def main(): print('---> Time:', datetime.datetime.now().time()) m = multiprocessing.Process(target=test_multiprocessing, args=(0,), daemon=True) m.start() # new process: __name__ = __mp_main__ when using Windows(Pycharm) # # Windows does not have fork print('---> Time:', datetime.datetime.now().time()) print('-' * 50) if __name__ == '__main__': # parent process only has __name__ = __main__ when using Windows(Pycharm) multiprocessing.freeze_support() # support Windows executable main() # ---> Time: 14:15:23.253697 # ---> Time: 14:15:23.264668 # -------------------------------------------------- | cs |
* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)
– 당신을 응원합니다. –
댓글
댓글 쓰기