Python readline 和 readlines 的行为

2 投票
2 回答
1339 浏览
提问于 2025-04-18 06:42

我在写一段小代码,目的是用 subprocess 来运行一个脚本,这个脚本会监听一些实时数据。

这是我的代码:

def subscriber():
    try:
        sub = subprocess.Popen('start listner', 
                               stdout=subprocess.PIPE, 
                               stderr=subprocess.Pipe)
    except Exception as e:
        print(e)
    return sub

def main():
    mysub = spark_subscriber()
    while True:
        # 1st version
        try:
            out = mysub.stdout.readline()
            print(out)
            sleep(1)
        # 2ndversion
        #try:
        #    out = mysub.stdout.readlines()  #notice the s
        #    print(out)
        #    sleep(1)
        # 3rd version
        #try:
        #    out = mysub.stdout.readlines()  #notice the s
        #    print(out)
        # 4th version
        #try:
        #    out = mysub.stdout.readline()
        #    print(out)
        except KeyboardInterrupt:
            exit_program(0)

第一个版本的代码是每次输出一行,然后暂停1秒,再输出下一行,直到所有内容都打印出来。

因为我想一次性打印所有的行,所以我把 readline() 改成了 readlines(),这样我就得到了第二个版本,我期待的输出是所有的行。结果发现不管我等多久,都没有任何输出。

补充:第三个版本也没有输出。

只有第四个版本是有效的。

我现在对 readline() 和 readlines() 的整个机制有点困惑。

有人能解释一下为什么 readlines() 不工作吗?

另外,如果 readlines() 在这种情况下可以工作,能不能给我一个有 sleep() 和没有 sleep() 的有效示例?

补充:我在这里犯了个大错误,第四个版本应该是有效的,第三个版本是不工作的。

2 个回答

0

我用一个HTML文件作为例子,来展示read()、readline()和readlines()这几个函数的具体用法。

chiru@python:~$ cat test.html
<html>
<head>
    <title>Feature information</title>
</head>
<body>
    <table border="1">
        <tr><th>Feature</th><th>Start</th><th>End</th></tr>
        <tr><td>a</td><td>a</td><td>a</td></tr>
        <tr><td>b</td><td>a</td><td>a</td></tr>
        <tr><td>v</td><td>a</td><td>a</td></tr>
    </table>
</body>
</html>
chiru@python:~$ 

打开Python控制台:

chiru@python:~$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more    information.

创建一个以读取模式打开的文件处理器:

>>> fh=open('test.html','r')
>>> fh
<open file 'test.html', mode 'r' at 0x7ff70408d4b0>

Read():这个函数会把整个文件当作一个字符串来读取。下面是用法示例。

>>> fh.read()
'<html>\n<head>\n <title>Feature information</title>\n</head>\n<body>\n<table border="1">\n<tr><th>Feature</th><th>Start</th><th>End</th></tr>\n<tr><td>a</td><td>a</td><td>a</td></tr>\n<tr><td>b</td><td>a</td><td>a</td></tr>\n<tr><td>v</td><td>a</td><td>a</td></tr>\n</table>\n</body></html>\n'
>>> 
>>> fh.read()
''

Readline():这个函数只会读取一行内容,作为一个字符串。下面是用法示例。

>>> fh=open('test.html','r')
>>> fh
<open file 'test.html', mode 'r' at 0x7ff70408d540>
>>> fh.readline()
'<html>\n'

Readlines():这个函数会把所有行读取成一个字符串列表

>>> fh.readlines()
['<head>\n', ' <title>Feature information</title>\n', '</head>\n', '<body>\n', '<table border="1">\n', '<tr><th>Feature</th><th>Start</th><th>End</th></tr>\n', '<tr><td>a</td><td>a</td><td>a</td></tr>\n', '<tr><td>b</td><td>a</td><td>a</td></tr>\n', '<tr><td>v</td><td>a</td><td>a</td></tr>\n', '</table>\n', '</body></html>\n']
>>> 
2

readlines 这个函数在读取数据时,会一直等到没有东西可以读取为止。所以,它会持续等待数据,直到数据的来源(你程序里的 start_listner)结束。

撰写回答