Вот пример кода:
Код: Выделить всё
import multiprocessing
from multiprocessing import Queue, Process
import time
class Parent:
def __init__(self):
print(multiprocessing.get_start_method())
self.child = Child()
def start(self):
self.child.start_child()
def print_child_variable(self):
print('Parent: Child Variable: ' + str(self.child.some_variable))
class Child:
def __init__(self):
self.some_variable = None
self.status = False
def start_child(self):
self.status = True
do_something_process = Process(target=self.do_something)
do_something_process.start()
def do_something(self):
self.some_variable = True
print('Child: some variable after changing: ' + str(self.some_variable))
def print_some_variable(self):
print('Child: some variable: ' + str(self.some_variable))
if __name__ == '__main__':
parent = Parent()
parent.print_child_variable()
time.sleep(1)
parent.start()
parent.print_child_variable()
time.sleep(1)
parent.print_child_variable()
time.sleep(1)
parent.child.print_some_variable()
Код: Выделить всё
fork
Parent: Child Variable None
Parent: Child Variable None
Child: some variable after chaging True
Parent: Child Variable None
Child: some variable None
Подробнее здесь: https://stackoverflow.com/questions/788 ... ent-object