Cam*_*phy 7 python recursion operating-system python-3.x
我编写了一个R.py脚本,其中包含以下两行:
import os
os.system("python3 R.py")
Run Code Online (Sandbox Code Playgroud)
我预计我的系统在运行此脚本几分钟后会耗尽内存,但它的响应速度仍然令人惊讶。有人知道,这里发生了什么样的Python解释器魔法吗?
os.system()实际上是对 C\xe2\x80\x99s 的调用system()。
以下是文档的说明:
\n\n\nsystem() 函数的行为应类似于使用 fork() 创建子进程,并且子进程使用 execl() 调用 sh 实用程序,如下所示:
\nexecl(, "sh", "-c", 命令, (char *)0);
\n其中 是 sh 实用程序的未指定路径名。未指定使用 pthread_atfork() 注册的处理程序是否在创建子进程的过程中被调用。
\nsystem() 函数应忽略 SIGINT 和 SIGQUIT 信号,并在等待命令终止时阻止 SIGCHLD 信号。如果这可能导致应用程序错过可能杀死它的信号,则应用程序应检查 system() 的返回值,并在命令因接收到信号而终止时采取适合应用程序的任何操作。
\nsystem() 函数不应影响除它自己创建的一个或多个进程之外的调用进程的任何子进程的终止状态。
\n在子进程终止之前,system() 函数不会返回。\n [选项结束]
\nsystem() 函数不需要是线程安全的。
\n
system()创建一个子进程并退出,没有要解析的堆栈,因此只要资源可用,人们就会期望它能够运行。此外,创建子进程的操作不是一个密集的操作\xe2\x80\x94,进程不会占用太多资源,但如果允许运行足够长的时间,脚本将开始影响总体性能,并最终内存不足,无法生成新的子进程。一旦发生这种情况,进程将退出。
为了演示这一点,将递归深度限制设置为 10 并允许程序运行:
\nimport os, sys, inspect\n\nsys.setrecursionlimit(10)\n\nargs = sys.argv[1:]\narg = int(args[0]) if len(args) else 0\n\nstack_depth = len(inspect.stack(0))\n\nprint(f"Iteration {arg} - at stack depth of {stack_depth}")\n\narg += 1\n\nos.system(f"python3 main.py {arg}")\n\nRun Code Online (Sandbox Code Playgroud)\n输出:
\nIteration 0 - at stack depth of 1 - avaialable memory 43337904128 \nIteration 1 - at stack depth of 1 - avaialable memory 43370692608 \nIteration 2 - at stack depth of 1 - avaialable memory 43358756864 \nIteration 3 - at stack depth of 1 - avaialable memory 43339202560 \nIteration 4 - at stack depth of 1 - avaialable memory 43354894336 \nIteration 5 - at stack depth of 1 - avaialable memory 43314974720 \nIteration 6 - at stack depth of 1 - avaialable memory 43232366592 \nIteration 7 - at stack depth of 1 - avaialable memory 43188719616 \nIteration 8 - at stack depth of 1 - avaialable memory 43173384192 \nIteration 9 - at stack depth of 1 - avaialable memory 43286093824 \nIteration 10 - at stack depth of 1 - avaialable memory 43288162304\nIteration 11 - at stack depth of 1 - avaialable memory 43310637056\nIteration 12 - at stack depth of 1 - avaialable memory 43302408192\nIteration 13 - at stack depth of 1 - avaialable memory 43295440896\nIteration 14 - at stack depth of 1 - avaialable memory 43303870464\nIteration 15 - at stack depth of 1 - avaialable memory 43303870464\nIteration 16 - at stack depth of 1 - avaialable memory 43296256000\nIteration 17 - at stack depth of 1 - avaialable memory 43286032384\nIteration 18 - at stack depth of 1 - avaialable memory 43246657536\nIteration 19 - at stack depth of 1 - avaialable memory 43213336576\nIteration 20 - at stack depth of 1 - avaialable memory 43190259712\nIteration 21 - at stack depth of 1 - avaialable memory 43133902848\nIteration 22 - at stack depth of 1 - avaialable memory 43027984384\nIteration 23 - at stack depth of 1 - avaialable memory 43006255104\n...\nRun Code Online (Sandbox Code Playgroud)\nhttps://replit.com/@pygeek1/os-system-recursion#main.py
\nhttps://pubs.opengroup.org/onlinepubs/9699919799/functions/system.html
\n| 归档时间: |
|
| 查看次数: |
192 次 |
| 最近记录: |