我有一个为我的覆盆子pi运行的python 2.7编写的python脚本。我尝试做的是继续我的步进电机,同时检查前方是否有正方形。为了使这些东西不干扰,我想将图像处理移动到它自己的过程中,并在进一步的步骤之前检查是否找到了图像。
这些是涉及的课程。
SharedImageDetectionValue
:
class SharedImageDetectionValue(object):
stored_has_position_found = False
def get_has_position_found(self):
return self.stored_has_position_found
def set_has_position_found(self, new_value):
self.stored_has_position_found = new_value
return self.stored_has_position_found
ImageProcessor
:
class ImageProcessor:
def __init__(self):
# constructor code
def run(self, shared):
# do image processing till found a square
shared.set_has_position_found(True)
Stepper
:
class Stepper(Observable):
def __init__(self):
# constructor code
def run_until_stopped(self, shared):
# some motor code
while self.running:
has_found = shared.get_has_position_found()
print("[ StepperH ] Value has found: " + str(has_found))
if not has_found:
self.do_steps(0.0005)
ProcessManager的:
class ProcessManager(BaseManager):
pass
我尝试用以下方法创建流程:
ProcessManager.register('SharedImageDetectionValue', SharedImageDetectionValue)
manager = ProcessManager()
manager.start()
shared = manager.SharedImageDetectionValue()
pool = Pool(1)
imgProcessor = ImageProcessor()
pool.apply_async(imgProcessor.run, args=(shared, ))
print("[ MAIN ] StepperH add to pool")
Stepper().run_until_stopped(shared)
出于测试目的,我在图像处理器中直接将值设置为true,但在步进器中它保持为False。
我也尝试将Stepper
添加到池中,但似乎步进器也没有运行。
此外,我尝试通过两个辅助函数调用进程,但效果类似:
def start_processing(passed_shared):
ImageProcessor().run(passed_shared)
def start_moving_to_target_platform(passed_shared):
StepperH().run_until_stopped(passed_shared)
我做错了什么?
答案 0 :(得分:0)
当您只想共享一个值时,为什么让自定义管理器复杂化?multiprocessing.Value
正是为此而存在的。此外,如果您只是使用它来运行单个流程,那么创建流程池是没有意义的。
您可以将代码大大简化为:
import ctypes
import multiprocessing
class ImageProcessor(multiprocessing.Process):
def __init__(self, shared):
super(ImageProcessor, self).__init__()
self.shared = shared
def run(self):
# do image processing till found a square
self.shared.value = True
class Stepper(Observable):
def run_until_stopped(self, shared):
while self.running:
print("[ StepperH ] Value has found: {}".format(shared.value)
if not shared.value:
self.do_steps(0.0005)
# rest of your Stepper code
if __name__ == "__main__": # a multiprocessing guard
shared = multiprocessing.Value(ctypes.c_bool, False)
img_processor = ImageProcessor(shared)
img_processor.start()
Stepper().run_until_stopped(shared)
不牺牲任何现有功能。