gpt4 book ai didi

python - 替代 __del__ 来获得最终结果并终止类中的进程?

转载 作者:行者123 更新时间:2023-12-01 02:41:35 27 4
gpt4 key购买 nike

这个虚拟类与我当前的项目类似:

class EndProcess(object):

def __init__(self):
self._Q = multiprocessing.Queue()

self.p1 = multiprocessing.Process(target=self.worker, args=("p1",))
self.p2 = multiprocessing.Process(target=self.worker, args=("p2",))
self.p3 = multiprocessing.Process(target=self.worker, args=("p3",))
self.p1.start()
self.p2.start()
self.p3.start()


def worker(self, name):
while True:
rnd = random.randint(0, 100)
self._Q.put((name, rnd))
print(name, rnd)
time.sleep(1)


def __del__(self):
# "log" final state of Queue
while not self._Q.empty():
print(self._Q.get())

# free resources
...

# clean up workers
self.p1.terminate()
self.p2.terminate()
self.p3.terminate()


if __name__ == "__main__":
ep = EndProcess()

问题是 __del__ 不能保证总是被调用,特别是在重要的时间点,因为 __del__ 仅在引用计数时被 Python 的垃圾收集器调用对象的值达到零。

我如何确保资源始终被释放并且工作人员始终被终止?

最佳答案

How can I assure that resources always get set free and the workers always get terminated?

您可以通过实现上下文管理器方法 __enter__ 来实现此目的和 __exit__ 。然后,您可以将 EndProcess 类与上下文管理器语句 with 一起使用。这样,即使发生错误,您的清理逻辑也会执行:

class EndProcess(object):
def __init__(self):
self._Q = multiprocessing.Queue()
self.p1 = multiprocessing.Process(target=self.worker, args=("p1",))
self.p2 = multiprocessing.Process(target=self.worker, args=("p2",))
self.p3 = multiprocessing.Process(target=self.worker, args=("p3",))
self.p1.start()
self.p2.start()
self.p3.start()

def worker(self, name):
while True:
rnd = random.randint(0, 100)
self._Q.put((name, rnd))
print(name, rnd)
time.sleep(1)

def __enter__(self):
# Add your setup logic here. Initialize any data structures.
# set any threads, etc.
pass

def __exit__(self, exc_type, exc_value, traceback):
# The three arguments to `__exit__` describe the exception
# caused the `with` statement execution to fail. If the `with`
# statement finishes without an exception being raised, these
# arguments will be `None`.
#
# These arguments may be useful for things such as logging, or
# debugging.
while not self._Q.empty():
print(self._Q.get())
...
self.p1.terminate()
self.p2.terminate()
self.p3.terminate()
...

现在您可以在上下文管理器语句中使用 EndProcess 实例:

end_process = EndProcess()
...
with end_process as ep:
# Use ep like you would normally. `__enter__` and `__exit__` will be
# automatically.
pass

关于python - 替代 __del__ 来获得最终结果并终止类中的进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45655448/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com