Python 多处理:"self."变量未更新in/by其他函数?
Python multiprocessing: "self."variable not updated in/by other functions?
看来我在这里运气不好...很抱歉问你们。 :(
我正在尝试执行以下操作:
import multiprocessing
import time
class TestClass(multiprocessing.Process):
def __init__(self):
super(TestClass, self).__init__()
print("Initializing the test class...")
self.VARIABLE = 0
def run(self):
while self.VARIABLE < 10:
print("Sleeping... Variable now: " + str(self.VARIABLE))
time.sleep(1)
def setVar(self, VALUE):
print("Setting new value from " + str(self.VARIABLE) + " to " + str(VALUE) + " ...")
self.VARIABLE = VALUE
if __name__ == "__main__":
TESTPROCESS = TestClass()
TESTPROCESS.start()
time.sleep(5)
TESTPROCESS.setVar(5)
time.sleep(5)
TESTPROCESS.setVar(10)
然而,在结果中,它不会更新 运行() 中的 self.VARIABLE,而只会更新 setVar() 中的 self.VARIABLE。
c:\Python35\python.exe Test.py
Initializing the test class...
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Setting new value from 0 to 5 ...
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Setting new value from 5 to 10 ...
Sleeping... Variable now: 0
Sleeping... Variable now: 0
[...]
我想,"self" 会表明,这是 "global" 个参数,用于此 class/object?
即使我修改 运行() 函数使其具有 "while true: --> break" 循环,同样的问题仍然出现。我的思维错误在哪里?
提前致谢...
TESTPROCESS.start()
causes the run()
method to be executed in a separate process; that's kind of the entire point. As a result, you do not have one TestClass
instance; you have two, and they exist in separate processes. One of these instances is updated by your calls to setVar
, and the other (due to being a different object) is not. If you want to be able to communicate between processes, look into pipes and queues.
看来我在这里运气不好...很抱歉问你们。 :(
我正在尝试执行以下操作:
import multiprocessing
import time
class TestClass(multiprocessing.Process):
def __init__(self):
super(TestClass, self).__init__()
print("Initializing the test class...")
self.VARIABLE = 0
def run(self):
while self.VARIABLE < 10:
print("Sleeping... Variable now: " + str(self.VARIABLE))
time.sleep(1)
def setVar(self, VALUE):
print("Setting new value from " + str(self.VARIABLE) + " to " + str(VALUE) + " ...")
self.VARIABLE = VALUE
if __name__ == "__main__":
TESTPROCESS = TestClass()
TESTPROCESS.start()
time.sleep(5)
TESTPROCESS.setVar(5)
time.sleep(5)
TESTPROCESS.setVar(10)
然而,在结果中,它不会更新 运行() 中的 self.VARIABLE,而只会更新 setVar() 中的 self.VARIABLE。
c:\Python35\python.exe Test.py
Initializing the test class...
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Setting new value from 0 to 5 ...
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Sleeping... Variable now: 0
Setting new value from 5 to 10 ...
Sleeping... Variable now: 0
Sleeping... Variable now: 0
[...]
我想,"self" 会表明,这是 "global" 个参数,用于此 class/object?
即使我修改 运行() 函数使其具有 "while true: --> break" 循环,同样的问题仍然出现。我的思维错误在哪里?
提前致谢...
TESTPROCESS.start()
causes the run()
method to be executed in a separate process; that's kind of the entire point. As a result, you do not have one TestClass
instance; you have two, and they exist in separate processes. One of these instances is updated by your calls to setVar
, and the other (due to being a different object) is not. If you want to be able to communicate between processes, look into pipes and queues.