0

I need a simple way to pass the stdout of a subprocess as a list to another function using multiprocess:

The first function that invokes subprocess:

def beginRecvTest():

  command = ["receivetest","-f=/dev/pcan33"]
  incoming = Popen(command, stdout = PIPE)
  processing = iter(incoming.stdout.readline, "")
  lines = list(processing)
  return lines

The function that should receive lines:

 def readByLine(lines):

  i = 0
  while (i < len(lines)):
    system("clear")

    if(lines[i][0].isdigit()):
        line = lines[i].split()
        dictAdd(line)
    else:
        next


    print ; print "-" *80
    for _i in mydict.keys():
        printMsg(mydict, _i)

    print "Keys: ", ;  print mydict.keys()
    print ; print "-" *80
    sleep(0.3)
    i += 1

and the main from my program:

if __name__ == "__main__":

    dataStream = beginRecvTest()
    p = Process(target=dataStream)
    reader = Process(target=readByLine, args=(dataStream,))
    p.start()
    reader.start()

I've read up on using queues, but I don't think that's exactly what I need.

The subprocess called returns infinite data so some people have suggested using tempfile, but I am totally confused about how to do this.

At the moment the script only returns the first line read, and all efforts on looping the beginRecvTest() function have ended in compilation errors.

vestland
  • 29,784
  • 17
  • 100
  • 189
Jalcock501
  • 359
  • 1
  • 5
  • 17
  • `list(processing)` won't return until eof (likely, until `receivetest` child process ends). Why do you use `multiprocessing.Process` here? The code looks like a copy-paste of several unrelated pieces without understanding what each piece should do. The title of your question implies that you want to get the whole subprocess' output at once (e.g., like `subprocess.check_output()` does), but the attempted solution suggests that you want to read the output incrementally while the subprocess is still running. See [read streaming input from .communicate()](http://stackoverflow.com/a/17698359/4279) – jfs Feb 06 '15 at 16:27
  • Please, clarify: do you want to get all output at once or do you want to read line-by-line from the child process while it is still running? – jfs Feb 06 '15 at 16:29
  • @J.F.Sebastian I want to be able to read the output line by line whilst the process is still running – Jalcock501 Feb 09 '15 at 08:55
  • have you read [the link that shows how to do it](http://stackoverflow.com/a/17698359/4279)? What do you want to do with the line? – jfs Feb 09 '15 at 14:19
  • @J.F.Sebastian I want to run both functions in parallel and pass the lines read in `beginRecvTest` to be processed in `readByLine` – Jalcock501 Feb 09 '15 at 14:26
  • both these functions are broken; drop them, start from scratch. You don't need `multiprocessing` here. `Popen` already starts a new process. Add whatever "readByLine()" should have done [into the loop from the code example that I've mentioned earlier](http://stackoverflow.com/a/17698359/4279) i.e., replace `print line,` with whatever you want to do with the line. – jfs Feb 09 '15 at 14:40
  • @J.F.Sebastian No worries I'll have another look at the code then. – Jalcock501 Feb 09 '15 at 14:51

0 Answers0