线程池的限制从何而来?

2024-04-28 12:11:42 发布

您现在位置:Python中文网/ 问答频道 /正文

使用下面的代码,我发现multiprocessing.pool.ThreadPool的最大大小是11689(在我的机器上)。如果我把它弄大了,我就会

RuntimeError: can't start new thread

有人能解释一下这是怎么来的吗?11689似乎是个奇怪的系统常数。。。所以也许我没有资源了?你知道吗

请注意:这与如何为线程池选择最佳线程数无关。这个问题是关于11689是从哪里来的?你知道吗

代码


Tags: 代码机器new系统常数资源线程multiprocessing
1条回答
网友
1楼 · 发布于 2024-04-28 12:11:42

简单看一下CPython的源代码处理threads表明,当线程创建失败时,解释器不会delving into details太多。它只是提高了你看到的RuntimeError。你知道吗

根据pthread_create手册页,线程创建可能会失败,并有以下错误代码和原因。你知道吗

EAGAIN Insufficient resources to create another thread.

EAGAIN A system-imposed limit on the number of threads was encountered. There are a number of limits that may trigger this error: the RLIMIT_NPROC soft resource limit (set via setrlimit(2)), which limits the number of pro‐ cesses and threads for a real user ID, was reached; the kernel's system-wide limit on the number of processes and threads, /proc/sys/kernel/threads-max, was reached (see proc(5)); or the maximum number of PIDs, /proc/sys/kernel/pid_max, was reached (see proc(5)).

...

我瞎猜你是在第一个问题上而不是在第二个问题上。分配新线程的内存可能不足。你知道吗

关于线程创建失败的更深入的解释可以在this link中找到。你知道吗

不过,这是瞎猜。解释器更详细的错误处理肯定会有所帮助。你知道吗

相关问题 更多 >