Hi all,

I’m struggling to understand why my Maya session deadlocks when leaving a
subprocess open whilst listening to its stdout in a thread.

The thread is deamonised, and without the thread then closing down Maya
works well.

I’ve narrowed down the problem into this.

import sysimport timeimport subprocessimport threading

child = """
import sys

while True:
    line = sys.stdin.readline()
    sys.stdout.write("child: Input: %s" % line)

"""

popen = subprocess.Popen(
    ["c:/python27/python.exe", "-u", "-c", child],
    stdout=subprocess.PIPE,
    stdin=subprocess.PIPE
)
def listen():
    def _listen(out):
        time.sleep(1)  # Give subprocess a second to launch
        print("parent: Listening..")
        for line in iter(out.readline, b""):
            sys.stdout.write(line)
        print("parent: Closing..")
        out.close()

    thread = threading.Thread(target=_listen, args=[popen.stdout])
    thread.daemon = True
    thread.start()
def send(text):
    popen.stdin.write(text + "\n")
    popen.stdin.flush()

listen()
send("hello")

This is on Windows, I haven’t tested other OSs yet, but would expect
different results there. I suspect the issue is an open handle to stdout,
but would still have expected the thread to forcefully exit and not make a
fuzz.

Any ideas?
​
-- 
*Marcus Ottosson*
[email protected]

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOAy2ViO-ZfoNBCju-yjxM5077pxp2xsaQA%2BeaEzuXaL7A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to