父级退出时的 Python 多处理队列
我的问题的要点是,当父进程(在这种情况下是守护进程)被杀死时,多处理队列会发生什么。
我有一个在后台运行的守护进程,它为子进程排队作业:
class manager(Daemon):
def run(self):
someQueue = MP.Queue()
someChild = MP.Process(target=someCode, args=(someArgs))
someChild.start()
...
如果管理器被杀死(假设它没有尝试使用 someQueue
并因此损坏了它,如文档中所述),有没有办法恢复队列中的数据?
我看到的两个理论上的解决方案是在退出该子进程之前清理 someChild
中的 someQueue
。另外转储队列以便我可以在管理器退出时恢复队列的状态也可以解决我的问题。然而,在实施任何一个之前,最好能被推动到正确的方向。
谢谢,
The gist of my question is what happens to a multiprocessing queue when the parent (a daemon in this circumstance) is killed.
I have a daemon that runs in the background which queues up jobs for child processes:
class manager(Daemon):
def run(self):
someQueue = MP.Queue()
someChild = MP.Process(target=someCode, args=(someArgs))
someChild.start()
...
If the manager is killed (assuming it wasn't trying to use someQueue
and therefore corrupted it as mentioned in the documentation), is there anyway to recover the data in the Queue?
Two theoretical solutions I see are cleaning up the someQueue
in someChild
before exiting this child process. Also dumping the queues so that I could restore the state of the queues when the manager exited would also solve my problem. However, before implementing either it would be nice to get nudged in the right direction.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您想要持久/可靠的排队。我相信 multiprocessing.Queue 类是通过管道实现的(就像使用 popen() 调用一样),因此数据相对短暂,您可能必须执行一些操作系统级别的技巧才能获取内容。您可能会考虑编写自己的持久队列类,该类使用文件系统文件(假设您的操作系统和文件系统支持锁定)来存储队列内容。然后,您可以提供您想要检查队列并恢复未处理数据的所有分析工具。
It sounds like you want persistent/reliable queuing. I believe the multiprocessing.Queue class is implemented with pipes (just like you would get with a popen() call), so the data is relatively transient and you'd probably have to do some OS-level trickery to grab the contents. You might look into writing your own persistent queue class that uses a filesystem file (assuming your OS and filesystem support locking) to store the queue contents. Then you can provide all of the analysis tools you desire to inspect the queue and recover unprocessed data.