

Python multiprocessing: how to limit the number of pending processes?
source link: https://www.codesd.com/item/python-multiprocessing-how-to-limit-the-number-of-pending-processes.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python multiprocessing: how to limit the number of pending processes?
When running a large number of tasks (with large parameters) using Pool.apply_async, the processes are allocated and go to a waiting state, and there is no limit for the number of waiting processes. This can end up by eating all memory, as in the example below:
import multiprocessing
import numpy as np
def f(a,b):
return np.linalg.solve(a,b)
def test():
p = multiprocessing.Pool()
for _ in range(1000):
p.apply_async(f, (np.random.rand(1000,1000),np.random.rand(1000)))
p.close()
p.join()
if __name__ == '__main__':
test()
I'm searching for a way to limit the waiting queue, in such a way that there is only a limited number of waiting processes, and Pool.apply_async is blocked while the waiting queue is full.
multiprocessing.Pool
has a _taskqueue
member of type multiprocessing.Queue
, which takes an optional maxsize
parameter; unfortunately it constructs it without the maxsize
parameter set.
I'd recommend subclassing multiprocessing.Pool
with a copy-paste of multiprocessing.Pool.__init__
that passes maxsize
to _taskqueue
constructor.
Monkey-patching the object (either the pool or the queue) would also work, but you'd have to monkeypatch pool._taskqueue._maxsize
and pool._taskqueue._sem
so it would be quite brittle:
pool._taskqueue._maxsize = maxsize
pool._taskqueue._sem = BoundedSemaphore(maxsize)
Recommend
-
19
Introduction It is natural that we would like to employ progress bars in our programs to show the progress of tasks. tqdm is one of my favorite progressing bar tools in Python. It could be easil...
-
20
Home Menu Python 3.8 新增 multiprocessing.SharedMemory 支持共...
-
11
The Complete Idiot's Guide to Refactoring Python Using Multiprocessing Pools Jul 31, 2020 While I would happil...
-
11
Python中的多进程(multiprocessing) 2018-11-21 22:22:14 +08 字数:2530 标签: Python Python中的多线程、包括协程,由于CPytho...
-
12
An error about multiprocessing of Python Our python program reported errors when running a new dataset: [77 rows x 4 columns]]'. Reason: 'error("'i' format requires -2147483648 <= number <= 2147483647",)' multipr...
-
18
MPIRE (MultiProcessing Is Really Easy) MPIRE, short for MultiProcessing Is Really Easy, is a Python package for multiprocessing, but faster and more user-friendly than the default multiprocessing package. It combines the...
-
8
python 包之 multiprocessing 多进程教程 原创 一、创建一个进程实例化 Process 类创建一...
-
8
使用Python Multiprocessing爬取知乎数据 NathanLVZS | Sunday 3 January 2016 Category: Python Python Multiprocessing Zhihu
-
11
A (not) short story about how I came to understand the discrete difference between multiprocessing and multithreading. Foreword What do you remember from 1991? It was full of interesting events: USSR collapsed and...
-
8
Understanding Multiprocessing and Multithreading in PythonAugust 22nd 2022 397 readsTrending #7...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK