I was writing a little piece of code that involves using subprocess to run a script that listens to some real time data

This is my code:

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)

The behavior of the first one output one line at a time, sleep for 1 second, and output the next line until everything is printed.

Since I want to print all the lines at once, I just changed the readline() into readlines() and got the second version, and my expected the output will be all the lines. - Turns out nothing is printed no matter how long i wait

Edit: And also no output for the 3rd version

The one that worked is the 4th

I'm a little bit confused over the whole mechanism behind the readline(), readlines() now.

Can someone please explain why readlines() don't work??

Also, if readlines() can work in situations like this, can someone provide a working example with sleep() and without sleep()?

EDIT: I made a big mistake here, the 4th version should be the one that's working, the 3rd version doesn't work

有帮助吗?

解决方案

readlines does not return a result until there is nothing left to read, so it will continue waiting for data until the producer of the data exits (start_listner in your program).

其他提示

I am taking a HTML file as an example to show the exact usage of read(), readline() and readlines(). showing the content of the file:

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:~$ 

Opening Python console:

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.

Creating a File handler in read mode:

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

Read(): This function reads whole file as a single string. Usage shown below.

>>> 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() : This function reads only one line as a single string. Usage shown below.

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

Readlines() : This function reads all the lines as a single list of strings.

>>> 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']
>>> 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top