我正在尝试编写一个程序,该程序可以多次应用某种功能。为了加快过程,而不是顺序执行,我尝试并行执行该函数。我在做什么,如下:
import multiprocessing
def start_process():
logger.debug('Starting {0}'.format(multiprocessing.current_process().name))
pool_size = len(inputs) if len(inputs) < multiprocessing.cpu_count() - 1 else multiprocessing.cpu_count() - 1
with multiprocessing.Pool(processes=pool_size, initializer=start_process) as pool:
for o, ipt in pool.imap_unordered(train_func, inputs):
output[(ipt[0], ipt[2])] = o
在上面的代码中,我使用参数initializer
以便能够跟踪应产生的进程数。此外,函数train_func
是运行优化的函数。
我正在任何时候最多具有32个处理器的服务器上运行代码。即使我希望“产生”的进程数最多可以达到31,但我可以看到产生了200-300多个进程,并且该程序最终崩溃了。
此外,我收到以下错误消息:
ERROR; return code from pthread_create() is 11
ERROR; return code from pthread_create() is 11
Error detail: Resource temporarily unavailable
Error detail: Resource temporarily unavailable
OMP: Error #34: System unable to allocate necessary resources for OMP thread:
OMP: System error #11: Resource temporarily unavailable
OMP: Hint Try decreasing the value of OMP_NUM_THREADS.
/bin/sh: fork: retry: No child processes
ERROR; return code from pthread_create() is 11
Error detail: Resource temporarily unavailable
/bin/sh: fork: retry: No child processes
OMP: Error #34: System unable to allocate necessary resources for OMP thread:
OMP: System error #11: Resource temporarily unavailable
OMP: Hint Try decreasing the value of OMP_NUM_THREADS.
您能否提供任何有关我确实可以限制产生的进程数量的提示?
答案 0 :(得分:0)
您可能应该使用min
内置函数。
此外,初始化代码必须包装在if __name__ == '__main__':
中,如多处理文档中所述。
所有类似的东西
import multiprocessing
def start_process():
logger.debug(
"Starting {0}".format(multiprocessing.current_process().name)
)
def train_func(*args):
pass
def main():
pool_size = min(len(inputs), multiprocessing.cpu_count() - 1)
with multiprocessing.Pool(
processes=pool_size, initializer=start_process
) as pool:
for o, ipt in pool.imap_unordered(train_func, inputs):
output[(ipt[0], ipt[2])] = o
if __name__ == "__main__":
main()