Wednesday 25 November 2020

Print realtime output of subprocess in jupyter notebook

In my file 'wrapper.py' I call a subprocess and print its output to stdout at realtime . This works just fine if I call the python script from the console. However, when calling it from a jupyter notebook the code hangs at the line proc.stdout.readline(). All previous print statements work fine..

proc = subprocess.Popen(["calc", "input.txt"], cwd=__dir, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
    out = proc.stdout.readline().decode("utf-8")
    err = proc.stderr.readline().decode("utf-8")
    if out == '' and err == '' and proc.poll() is not None:
        break
    if out:
        print("::::%s"%(out), end='')
    if err:
        print("::::%s"%(err), end='', file=sys.stderr)
rc = proc.poll()
print("---> returns %s"%(rc))

Does anyone have an idea on how to fix this?



from Print realtime output of subprocess in jupyter notebook

No comments:

Post a Comment