简体   繁体   English

python os.fork()可以创建多少个进程

[英]how many processes can be created with python os.fork()

If I do this below: 如果我在下面这样做:

for i in range(10000):
    os.fork()

What's going on? 这是怎么回事? don't consider reaping.. I just wanna know about os.fork() can make how much processes in linux, if it 's just like windows, only can make about 2000 processes, how the next 8000 processes will do? 我只是想了解os.fork()可以在linux中创建多少个进程,如果它像Windows一样,只能创建大约2000个进程,那么接下来的8000个进程将如何呢?
Thanks. 谢谢。

os.fork() spawns a new OS-level process. os.fork()产生一个新的OS级进程。 Any limits on the number of those aren't dependent on Python, but on the operating system. 数量的任何限制都不取决于Python,而取决于操作系统。 According to this previous question , on Linux you can find out about any software-imposed constraints on that by looking at: 根据上一个问题 ,在Linux上,您可以通过查看以下内容了解有关此软件的任何约束:

cat /proc/sys/kernel/pid_max

but that might be further restrained by /etc/security/limits.conf . 但这可能会受到/etc/security/limits.conf进一步限制。 If those don't hit you first, you will eventually run into problems with available hardware resources - your code is a fork bomb , and type of trivial denial of service attack (any software limits on the number of processes are set to avoid this kind of attack). 如果没有首先遇到这些问题,您最终将遇到可用硬件资源的问题-您的代码是fork炸弹 ,并且是琐碎的拒绝服务攻击类型(对进程数量设置了任何软件限制都可以避免这种情况)的攻击)。

The code doesn't do what you think it does. 该代码不执行您认为的操作。

When you're forking, you're actually duplicating the current process. 当您分叉时,实际上是在复制当前过程。

This means that after forking, both processes will continue the iteration from the same point. 这意味着在分叉之后, 两个过程都将从同一点继续迭代。 And then both will fork, and so on... The number of created threads will be exponential . 然后两者都会分叉,依此类推...创建的线程数将是指数的

Example: 例:

import os
for i in range(3):
    os.fork();

print "T"

When executed, you should see 2^3 = 8 processes. 执行后,您应该看到2 ^ 3 = 8个进程。

Just to return to your original question, since fork creates processes, the limit on the number of threads is irrelevant. 只是回到您的原始问题,因为fork创建了进程,所以线程数量的限制是无关紧要的。

The os.fork() function creates a copy of the calling process. os.fork()函数创建调用过程的副本。 Threads are created in Python by using the threading module. 线程是通过使用threading模块在Python中创建的。

The amount of processes you can create on a UNIX-like system such as Linux is generally limited by the amount of memory the computer has and certain limits set in the operating system. 您可以在类似UNIX的系统(例如Linux)上创建的进程数量通常受计算机拥有的内存数量和操作系统中设置的某些限制的限制。 The output of the command ulimit -a shows all the limits. 命令ulimit -a的输出显示所有限制。

The amount of threads you can create within a process is usually limited by the amount of stack space that the process has and the maximal amount of (virtua)l memory that the process is allowed to have. 您可以在进程内创建的线程数量通常受该进程具有的堆栈空间数量以及该进程允许具有的(虚拟)内存的最大数量限制。 These limits are also shown by ulimit . 这些限制也由ulimit

Note that when you are using the standard python implementation (aka CPython), only one thread at a time can be executing Python bytecode. 请注意,当您使用标准python实现(即CPython)时,一次只能有一个线程在执行Python字节码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM