sách gpt4 ai đã đi

Python - Nhóm công nhân chạy liên tục

In lại 作者:行者123 更新时间:2023-12-04 07:22:30 29 4
mua khóa gpt4 Nike

Tôi đang sử dụng multiprocessor.Pool并行处理一些文件。该代码等待接收文件,然后使用 Pool.apply_async 将该文件发送给工作人员。 ,然后处理文件。
这段代码应该一直在运行,因此我从不关闭池。然而,这会导致池随着时间的推移消耗大量内存。
Mã như sau:

if __name__ == "__main__":
with Pool(processes=PROCESS_COUNT) as pool:
trong khi Đúng:
f = wait_for_file()
pool.apply_async(process_file, (f,))
如何在不关闭池的情况下防止发生高内存使用率?

1 Câu trả lời

是的,如果你分配资源并且你不释放它们,不管是 spawned processes 的数量或者只是(一大块)内存,在您或您的系统自愿或强制解除分配之前,您机器上用于其他任务的资源将减少。
您可能想使用 maxtasksperchild Pool 的论据试图杀死奴隶,例如如果他们分配了内存并且您在某处发生了泄漏,那么您至少可以节省一些资源。

Note: Worker processes within a Pool typically live for the complete duration of the Pool’s work queue. A frequent pattern found in other systems (such as Apache, mod_wsgi, etc) to free resources held by workers is to allow a worker within a pool to complete only a set amount of work before being exiting, being cleaned up and a new process spawned to replace the old one. The maxtasksperchild argument to the Pool exposes this ability to the end user.


或者,不要推出自己的 Pool 实现因为在你到达那里之前,它会是马车,你会不必要地浪费时间。 相反 使用例如 Celery ( tutorial ) 甚至希望对讨厌的极端情况进行测试,您可能会花费比必要的更多时间。
或者,如果您想尝试一下, here is a similar question它提供了自定义从属池管理的步骤。

关于python - 不断运行的 worker 池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68411908/

29 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com