Jython:subprocess.Popen用完了文件描述符 [英] Jython: subprocess.Popen runs out of file descriptors

查看:273
本文介绍了Jython:subprocess.Popen用完了文件描述符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python的Jython 2.51实现来编写一个脚本,它通过 subprocess.Popen 反复调用另一个进程,并使用 PIPE 到pipe stdout stderr 到父进程, stdin 到子进程。经过几百次循环迭代,我似乎用尽了文件描述符。

Python子流程文档除了 close_fds 选项之外,没有提到非常清楚地描述释放文件描述符的细节(为什么除了0,1和2之外还有任何文件描述符打开第一个地方?)。我假设在CPython中,引用计数处理资源释放问题。在Jython中使用 Popen 对象完成所有描述符的释放的正确方法是什么?



编辑:以防万一,这是一个多线程的程序,所以有几个 Popen 进程同时运行。



在UNIX上,这是使用fork的副作用,它重复了当前进程以及加载新的可执行文件之前的所有句柄。在Windows上,它是更明确的,但Python无论如何,尝试尽可能匹配跨平台的行为。



close_fds选项,当True,关闭所有这些在产生子进程之后继承了句柄,所以新的可执行文件以干净的版本开始。但是,如果你的子进程一次只运行一次,并在完成时终止,那么这应该不是问题。


I'm using the Jython 2.51 implementation of Python to write a script that repeatedly invokes another process via subprocess.Popen and uses PIPE to pipe stdout and stderr to the parent process and stdin to the child process. After several hundred loop iterations, I seem to run out of file descriptors.

The Python subprocess documentation mentions very little about freeing file descriptors, other than the close_fds option, which isn't described very clearly (Why should there be any file descriptors besides 0, 1 and 2 open in the first place?). I'm assuming that in CPython, reference counting takes care of the resource freeing issue. What's the proper way to make sure all descriptors get freed when one is done with a Popen object in Jython?

Edit: Just in case it makes a difference, this is a multithreaded program, so there are several Popen processes running simultaneously.

解决方案

This only answers part of your question, but my understanding is that, when you spawn a new process, it normally inherits all the handles of the parent process. That includes such things as open files and sockets that you're listening on.

On UNIX, that's a side-effect of using 'fork', which duplicates the current process and all of its handles before loading the new executable. On Windows it's more explicit, but Python does it anyway, to try to match the behavior across platforms as much as possible.

The close_fds option, when True, closes all these inherited handles after spawning the subprocess, so the new executable starts with a clean slate. But if your subprocesses are run one at a time, and terminating when they're done, then this shouldn't be the problem.

这篇关于Jython:subprocess.Popen用完了文件描述符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆