本文整理汇总了Python中multiprocessing.Process.cancel方法的典型用法代码示例。如果您正苦于以下问题:Python Process.cancel方法的具体用法?Python Process.cancel怎么用?Python Process.cancel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类multiprocessing.Process
的用法示例。
在下文中一共展示了Process.cancel方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from multiprocessing import Process [as 别名]
# 或者: from multiprocessing.Process import cancel [as 别名]
class Viewer:
def __init__(self):
# create a pipe to communicate with the child
(self.pipe, self.child_pipe) = Pipe()
# create the subprocess
self.child = Process(target=self._handler)
# set the child to run as a background process (i.e., exit when parent does)
self.child.daemon = True
self.child.start()
def update(self, knowledge_base, robot_state):
self.pipe.send((knowledge_base, robot_state))
# path = '/tmp/transfer.pickle'
# pickle.dump((knowledge_base, robot_state), open(path, 'wb'))
# self.pipe.send(path)
def close(self):
# signal the child to close
self.pipe.close()
# wait a bit for the process to exit
self.child.wait(1)
# kill the process if it doesn't exit normally
if self.child.is_alive():
logger.warn("Viewer escalating to kill child")
self.child.cancel()
self.child = None
logger.info("Viewer closed")
def _handler(self):
logger.debug("child {} start".format(self.child.pid))
viewer = RemoteKnowledgeBaseViewer(self.child_pipe)
viewer.run()
@property
def heartbeat(self):
logger.info("checking for heartbeat")
if self.pipe.poll():
logger.info("heartbeat present")
self.pipe.recv()
logger.info("heartbeat read")
return True
else:
return False