from multiprocessing import Process
class Test():
def __init__(self):
pass
def method(self):
self.vari = 0
if __name__=='__main__':
test_1 = Test()
test_2 = Test()
th_1 = Process(target=test_1.method)
th_2 = Process(target=test_2.method)
th_1.start()
th_2.start()
th_1.join()
th_2.join()
print(test_1.vari)
print(test_2.vari)
왜 'Test' object has no attribute 'vari' 라고 할까요...ㅠㅠ
method 에서 self.vari = 0 하는데...
많은 가르침 부탁드립니다!
OS에서 쓰레드와 프로세스(fork)는 둘이 다르고, 가장 대표적으로는 프로세스들끼리 변수를 공유하지 않다는 점이 가장 큽니다.
각각 오브젝트의 vari는 해당 프로세스에서만 존재하고, print()함수를 돌리는 메인 프로세스에서는 존재하지 않는 변수(멤버)입니다.
답변 감사합니다!
다른 방식으로 값을 리턴 받도록 해보겠습니다.
좋은 밤되시고 늘 좋은 일만 가득하세요!