Linux 上的 Python 从管道读取行

发布于 2024-11-11 04:01:17 字数 364 浏览 4 评论 0原文

当使用 os.pipe() 创建管道时,它返回 2 个文件号;一个读端和一个写端,可以通过os.write()/os.read()进行写入和读取;没有 os.readline()。可以使用readline吗?

import os
readEnd, writeEnd = os.pipe()
# something somewhere writes to the pipe
firstLine = readEnd.readline() #doesn't work; os.pipe returns just fd numbers

简而言之,当您只有文件句柄号时,是否可以使用 readline ?

When creating a pipe with os.pipe() it returns 2 file numbers; a read end and a write end which can be written to and read form with os.write()/os.read(); there is no os.readline(). Is it possible to use readline?

import os
readEnd, writeEnd = os.pipe()
# something somewhere writes to the pipe
firstLine = readEnd.readline() #doesn't work; os.pipe returns just fd numbers

In short, is it possible to use readline when all you have is the file handle number?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

℡Ms空城旧梦 2024-11-18 04:01:17

您可以使用 os.fdopen() 从文件描述符中获取类文件对象。

import os
readEnd, writeEnd = os.pipe()
readFile = os.fdopen(readEnd)
firstLine = readFile.readline()

You can use os.fdopen() to get a file-like object from a file descriptor.

import os
readEnd, writeEnd = os.pipe()
readFile = os.fdopen(readEnd)
firstLine = readFile.readline()
十级心震 2024-11-18 04:01:17

将管道从 os.pipe() 传递到 os.fdopen(),它应该从文件描述符构建一个文件对象。

Pass the pipe from os.pipe() to os.fdopen(), which should build a file object from the filedescriptor.

裸钻 2024-11-18 04:01:17

听起来您想获取一个文件描述符(数字)并将其转换为文件对象。 fdopen 函数应该做到这一点:

import os
readEnd, writeEnd = os.pipe()
readFile = os.fdopen(readEnd)
# something somewhere writes to the pipe
firstLine = readFile.readline()

现在无法测试,如果不起作用请告诉我。

It sounds like you want to take a file descriptor (number) and turn it into a file object. The fdopen function should do that:

import os
readEnd, writeEnd = os.pipe()
readFile = os.fdopen(readEnd)
# something somewhere writes to the pipe
firstLine = readFile.readline()

Can't test this right now so let me know if it doesn't work.

夕嗳→ 2024-11-18 04:01:17

os.pipe() 返回文件描述符,因此您必须像这样包装它们:

readF = os.fdopen(readEnd)
line = readF.readline()

有关更多详细信息,请参阅 http://docs.python.org/library/os.html#os.fdopen

os.pipe() returns file descriptors, so you have to wrap them like this:

readF = os.fdopen(readEnd)
line = readF.readline()

For more details see http://docs.python.org/library/os.html#os.fdopen

萌面超妹 2024-11-18 04:01:17

我知道这是一个老问题,但这是一个不会陷入僵局的版本。

import os, threading

def Writer(pipe, data):
    pipe.write(data)
    pipe.flush()


readEnd, writeEnd = os.pipe()
readFile = os.fdopen(readEnd)
writeFile = os.fdopen(writeEnd, "w")

thread = threading.Thread(target=Writer, args=(writeFile,"one line\n"))
thread.start()
firstLine = readFile.readline()
print firstLine
thread.join()

I know this is an old question, but here is a version that doesn't deadlock.

import os, threading

def Writer(pipe, data):
    pipe.write(data)
    pipe.flush()


readEnd, writeEnd = os.pipe()
readFile = os.fdopen(readEnd)
writeFile = os.fdopen(writeEnd, "w")

thread = threading.Thread(target=Writer, args=(writeFile,"one line\n"))
thread.start()
firstLine = readFile.readline()
print firstLine
thread.join()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文