问题描述
我尝试扩展multiprocessing.Process
类以以命令模式的方式使用它...有一个调度程序实例,客户端在其中调用命令并调用执行。但是在调用self.execute()
之后,命令代码永远不会终止。这是命令类:
class Command(Process):
def __init__(self):
super().__init__()
self.result = None
self.command_name = type(self).__name__
self.shell = False
#from Process
def run(self):
super().run()
print("running "+self.command_name)
sys.stdout.flush()
self.execute()
print("finished "+self.command_name)
sys.stdout.flush()
sys.exit(0)
def execute(self):
pass
想法很简单,Command的每个子类在execute()方法中提供自己的代码。例如:
class LoadCommand(Command):
def __init__(self,parametera,...):
super().__init__()
...
def execute(self):
print("executing LoadCommand")
....
return
这是我的调度程序:
class Scheduler:
_instance = None
_history_queue = []
_command_queue = []
_logger = None
#IPC,negative maxsize means infinite size
_pipe = Queue(maxsize=-1)
def __init__(self):
raise RuntimeError('Call getInstance() instead')
@classmethod
def getInstance(cls):
if cls._instance is None:
cls._instance = cls.__new__(cls)
return cls._instance
def getPipe(self):
print(self._pipe)
return self._pipe
def enqueueCommand(self,command):
# if isinstance(command,Command):
self._command_queue.append(command)
def executeQueue(self,synchronicMode):
while len(self._command_queue) > 0:
command = self._command_queue.pop(0)
command.start()
if synchronicMode:
#wait until this process is done
print("Waiting\n")
command.join(10)
if command.is_alive():
print("process isn't finished")
else:
print("process finished")
self._history_queue.append(command)
在运行成功开始(过程终止)之后,我尝试立即调用sys.exit(0)。因此,继承层次结构中可能有一个错误,但我看不到它。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)