在python中使用多个输出流? [英] Use multiple output stream in python?

查看:183
本文介绍了在python中使用多个输出流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是在python函数中创建多个输出流,并将它们称为 1 2 3 .....:
test.py

What I am going to do is to create multiple output steams in a python function, and refer them as 1, 2, 3.....: In test.py:

def main():
  ...
  print >>fd1, 'words1'
  print >>fd2, 'words2'
  print >>fd3, 'words3'
  ...

使用时重定向:

python test.py 1>1.txt 2>2.txt 3>3.txt

这些文件的内容:

1.txt ->  words1
2.txt ->  words2
3.txt ->  words3

问题是,如何创建 fd1 fd2 fd3

The question is, how to create those fd1, fd2, fd3?

已添加:

我用过这个:

outfiles = {}  
for _ in range(3): 
    fd = os.dup(1)
    outfiles[fd] = os.fdopen(fd, 'w')

def main():
  for no in outfiles: 
    print >>outfiles[no], "foo"
    print >>outfiles[no], outfiles[no].fileno()

但结果取决于我的执行方式此代码:

But the results depends on how I execute this code:

eg1:

python test.py

foo
3
foo
4
foo
5

eg2:

python test.py 3>log.txt


foo
4
foo
5
foo
6

eg3:

python test.py 1>log.txt

Nothing printed

所以我猜,输出实际上是 1 ,如果文件描述符已经在执行中使用过(例如: python test.py 3> log.txt ),os.dup(1)将不再返回它。

So I guess, the output is actually 1, and if a file descriptor have already been used in execute (eg:python test.py 3>log.txt), the os.dup(1) won't return it anymore.

推荐答案

在Linux上,您希望存在于 / proc / self / fd / 中的文件句柄。例如:

On Linux, the file handles that you want exist in /proc/self/fd/. For example:

with open('/proc/self/fd/1', 'w') as fd1, open('/proc/self/fd/2', 'w') as fd2, open('/proc/self/fd/3', 'w') as fd3:
   print >>fd1, 'words1'
   print >>fd2, 'words2'
   print >>fd3, 'words3'

在其他一些unices上,您可以在 / dev / fd 下找到类似的文件句柄。

On some other unices, you may find similar file handles under /dev/fd.

现在,您可以运行命令并验证输出文件是否符合要求:

Now, you can run your command and verify that the output files are as desired:

$ python test.py 1>1.txt 2>2.txt 3>3.txt
$ cat 1.txt
words1
$ cat 2.txt
words2
$ cat 3.txt
words3



打开文件描述符数量的限制



操作系统对进程可能具有的最大打开文件描述符数量进行限制。有关此问题的讨论,请参阅文件描述符数量的限制

使用bash编号的文件描述符时,限制要严格得多。在bash下,只为用户保留最多9个文件描述符。使用更高的数字可能会导致与bash的内部使用冲突。从 man bash

When using bash's numbered file descriptors, the restrictions are much tighter. Under bash, only file descriptors up to 9 are reserved for the user. The use of higher numbers may cause conflict with bash's internal use. From man bash:


使用大于9的文件描述符的重定向应该与
care,因为它们可能与文件描述符冲突,shell在内部使用

Redirections using file descriptors greater than 9 should be used with care, as they may conflict with file descriptors the shell uses internally.

如果,按照注释,你想分配数百个文件描述符,然后不要在 / proc / self / fd 中使用shell重定向或编号描述符。相反,使用python的open命令,例如直接在您想要的每个输出文件上打开('255.txt','w')

If, as per the comments, you want to assign hundreds of file descriptors, then don't use shell redirection or the numbered descriptors in /proc/self/fd. Instead, use python's open command, e.g. open('255.txt', 'w') directly on each output file that you want.

这篇关于在python中使用多个输出流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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