Python打开一个命名管道==挂? [英] Python open a named pipe == hanging?

查看:219
本文介绍了Python打开一个命名管道==挂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我刚发现一般的打开文件机制对命名管道(fifo)不起作用。假设我写了这样的东西,它就是

只需挂起python:


#!/ usr / bin / python


import os


os.mkfifo(''my_fifo'')


open(''my_fifo'',''r +' ')。write(''some strings。'')

x = os.popen(''cat my_fifo'')。read()


打印x

我知道我可以在这个非常简单的情况下使用tempfile而不是fifo,我只是想知道是否有标准方式

使用python处理fifos。特别是非常重要的情况

,当我想调用类似系统程序的diff时,需要两个

文件作为输入。假设我有两个python字符串对象A和B,我想要调用diff以查看这两个字符串之间有什么不同,并将查找返回为字符串obj C回到python。

这当然可以用这种方式完成:


open(''tmpfile1'',''w'')。写(A)

open(''tmpfile2'',''w'')。写(B)

C = os.popen(''diff tmpfile1 tmpfile2 '')。读()


但这有点尴尬不是吗? :-) Bash这样做的方式

就是(假设A是prog2的标准输出,B是标准输出的
prog3):


diff<(prog2)<(prog3)C


在Python中执行此操作的最佳方法是什么?


谢谢!

Hi,

I just found out that the general open file mechanism doesn''t work
for named pipes (fifo). Say I wrote something like this and it
simply hangs python:

#!/usr/bin/python

import os

os.mkfifo(''my_fifo'')

open(''my_fifo'', ''r+'').write(''some strings.'')
x = os.popen(''cat my_fifo'').read()

print x
I know I could use a tempfile instead of a fifo in this very
simple case, I just want to know is there a standard way of
handling fifos withing python. Especially the non-trivial case
when I want to call a diff like system program which takes two
files as input. Say I have two python string objects A and B, I
want to call diff to see what is the different between those two
strings, and return the finding as a string obj C back to python.
This certainly can be done in this way:

open(''tmpfile1'', ''w'').write(A)
open(''tmpfile2'', ''w'').write(B)
C = os.popen(''diff tmpfile1 tmpfile2'').read()

But that''s kinda awkward isn''t it? :-) The Bash way of doing this
would be (suppose A is the stdout of prog2, B is the stdout of
prog3):

diff <(prog2) <(prog3) C

What is the best way of doing this in Python?

Thank you!

推荐答案

罗切斯特在新闻中写道:op *************** @ cadet .mshome.net在

comp.lang.python:
Rochester wrote in news:op***************@cadet.mshome.net in
comp.lang.python:

我刚发现一般的打开文件机制不起作用

用于命名管道(fifo)。假设我写了这样的东西,它就是

只需挂起python:


#!/ usr / bin / python


import os


os.mkfifo(''my_fifo'')


open(''my_fifo'',''r +' ')。write(''some strings。'')

x = os.popen(''cat my_fifo'')。read()
I just found out that the general open file mechanism doesn''t work
for named pipes (fifo). Say I wrote something like this and it
simply hangs python:

#!/usr/bin/python

import os

os.mkfifo(''my_fifo'')

open(''my_fifo'', ''r+'').write(''some strings.'')
x = os.popen(''cat my_fifo'').read()



您可能需要使用os.open()

w = os.open(''my_fifo'',os.O_WRONLY)

r = os.open(''my_fifo'',os.O_RDONLY)


虽然我现在正在使用Windows,所以无法测试它。

You will probably need to use os.open()

w = os.open( ''my_fifo'', os.O_WRONLY )
r = os.open( ''my_fifo'', os.O_RDONLY )

though I''m using windows now, so can''t test it.


打印x


我知道我可以在这个非常简单的情况下使用tempfile而不是fifo,我只是想知道有没有一种标准的方法来处理带有python的fifos。
print x
I know I could use a tempfile instead of a fifo in this very
simple case, I just want to know is there a standard way of
handling fifos withing python.



参见os.pipe()


Rob。

-
http://www.victim-prime.dsl.pipex.com/


Rochester< ro *********** @ gmail.comwrote:
Rochester <ro***********@gmail.comwrote:




我刚刚发现一般的打开文件机制对命名管道(fifo)不起作用。
。假设我写了这样的东西,它就是

只需挂起python:
Hi,

I just found out that the general open file mechanism doesn''t work
for named pipes (fifo). Say I wrote something like this and it
simply hangs python:



....就像它会挂起一样任何其他语言......! - )。看起来你可能不会完全认识到fifos的语义 - 可以理解的是,

因为大多数系统上的手册页对这个主题并不是太清晰。

但是有一个很好的,例如,在

< http://opengroup.org/onlinepubs/007908799/xsh/open.html,我

有选择地引用...:


""

O_RDWR

供开放阅读和写作。如果此标志是应用于FIFO的
,则结果是不确定的。

"""


(这意味着你打开r + _might_会导致系统犯下

恶魔飞出你的鼻子,例如

< http://everything2.com/index.pl ?NODE_ID = 922462 ... :-);所以,_don''t_使用

''r +''打开你的fifo。但是,此外......:


""

O_NONBLOCK

使用O_RDONLY或O_WRONLY打开FIFO时set:如果设置了O_NONBLOCK:


仅用于读取的open()将立即返回。如果没有进程当前有文件

打开阅读,则只打开()for

写入将返回错误。

如果O_NONBLOCK清除:


只读取的open()将阻塞调用线程,直到线程

打开文件进行写入。仅用于写入的open()将阻止

调用线程,直到线程打开文件进行读取。

"""

这最后一段是至关重要的:

的基本语义Unix中的FIFO用于写入和读取过程(或更现代的

线程 ; :-) torendezvous围绕他们各自的打开(2),

阻塞,直到他们都达到开放点。

非阻塞情况下的语义有些奇怪(以我的思维方式,但是,然后,我的Unix背景_有点过时了!)但是他们无论如何我都不会帮助你

- 看起来你会喜欢完全不同的

语义(既没有打开阻止,也没有阅读一个),但是

Unix并没有真正提供给他们......

Alex

....just as it would "hang" any other language...!-). Looks like you may
not be fully cognizant of the semantics of fifos -- understandably,
because the man pages on most systems are not too limpid on the subject.
But there''s a good one, e.g., at
<http://opengroup.org/onlinepubs/007908799/xsh/open.html, and I
selectively quote...:

"""
O_RDWR
Open for reading and writing. The result is undefined if this flag is
applied to a FIFO.
"""

(which means that your open with r+ _might_ cause the system to make
demons fly out of your nose, according to e.g.
<http://everything2.com/index.pl?node_id=922462...:-); so, _don''t_ use
''r+'' to open your fifo. But, moreover...:

"""
O_NONBLOCK
When opening a FIFO with O_RDONLY or O_WRONLY set: If O_NONBLOCK is set:

An open() for reading only will return without delay. An open() for
writing only will return an error if no process currently has the file
open for reading.
If O_NONBLOCK is clear:

An open() for reading only will block the calling thread until a thread
opens the file for writing. An open() for writing only will block the
calling thread until a thread opens the file for reading.
"""

This last paragraph is the crucial one: the fundamental semantics of
FIFOs in Unix is for the writing and reading processes (or more modernly
"threads":-) to "rendezvous" around their respective calls to open (2),
blocking until both of them reach the open point. The semantics in the
nonblocking case(s) are somewhat weird (to my way of thinking, but then,
my Unix background _is_ somewhat dated!-), but they wouldn''t help you
anyway, it seems to me -- looks like you''d like drastically different
semantics (with neither open blocking, or just the reading one), but
Unix just doesn''t really offer them...
Alex


谢谢为你的建议。所以,事实证明,在Python编程中,fifos毫无用处,这对我来说是非常令人失望的:-(


我不是说我_have to_使用fifo,毕竟这是一个相当奇怪的

自从最后一次冰封以来没有出现过的......我只是很失望

老朴素Bash的事实似乎在这个特殊的

方面优于Python。


我是Python的新手,在Bash编程方面更加舒适.A

这样的简单Bash脚本可以利用fifo,因此

减少了不必要的临时文件创建的开销:


#!/ bin / bash


mkfifo my_fifo

echo"这是我的fifo中的一个字符串!" my_fifo&

cat my_fifo

rm my_fifo


不是很整洁吗?


无论如何,我认为每种脚本语言都有它的优点和利弊Bash是

在处理fif时可能更灵活操作系统和多路管道(通过

过程替换的神奇机制)。


谢谢!

8月3日星期四2006 22:13:56 -0400,Alex Martelli< al *** @ mac.comwrote:
Thank you for your advise. So, it turns out that fifos are quite useless
in Python programming then, which is quite disappointing to me :-(

I am not saying that I _have to_ use fifo, afterall it is a rather odd
thingy not in fasion since the last iceage... I am just disappointed by
the fact that the old plain Bash seems to excel Python in this special
aspect.

I am new to Python and much more comfortable in Bash programming. A
simple Bash script like this would take the advantage of a fifo, hence
reduce the overhead of unneccesarry temporary files creation:

#!/bin/bash

mkfifo my_fifo
echo "this is a string in my fifo!" my_fifo &
cat my_fifo
rm my_fifo

Isn''t it neat?

Anyway, I think every scripting language has its pros and cons. Bash is
probably more flexible in dealing with fifos and multiway pipes (through
the magic menchanism of process substitution).

Thank you!
On Thu, 03 Aug 2006 22:13:56 -0400, Alex Martelli <al***@mac.comwrote:


这篇关于Python打开一个命名管道==挂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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