java - Python - communicate with subprocess -
i'm running python 2.7 on win32 os, i'm hoping write platform-independent code. i'm trying use python interact in real-time java program wrote, , figured pipes best way this. python script calling java subprocess. essentially, java gui , python end. (i don't want use jython or wxpython because want dependent upon standard libraries each language provides.) trying set communication between 2 has been terrible. can send message (parent) python script (child) java class using
process.stdin.write(msg) process.stdin.flush()  but reading java's output has not worked. use
process.stdout.read()  but apparently blocks forever if there's nothing read. , process.communicate() off limits because doesn't until subprocess terminates. according research, common method people use around problem "use threads" (although suggested appending newline when writing -- didn't work), being new python , threading in general have no idea how look. i've tried looking on standard library's subprocess.py source hasn't helped. there way see if stdout empty, @ least? if not, how accomplish this?
process.stdout.read()
apparently blocks forever if there's nothing read.
well not exactly, block while either reading/waiting until hits eof set when file closes, 1 way circumvent stating how many bytes want read process.stdout.read(1) read 1 byte , return if theres no byte again wait until theres @ least 1 byte or eof.
you may use python select module has optional timeout period select waits long or returns empty values http://docs.python.org/library/select.html
though may not supported on windows.
(although suggested appending newline when writing -- didn't work)
i've done though from/to python, coupled process.stdout.readline().rstrip() data set of line(s) though still have strip them, due note you may have flush in order both processes register data.
i did find java: how both read , write & process thru pipe (stdin/stdout) may you.
good luck.
Comments
Post a Comment