How to send multiple string by stdin in python -
i send string router_status[router]='on' parent code new process
proc[router] = subprocess.popen([sys.executable, os.getcwd() + '/' + router + '.py', router, json.dumps(graph), json.dumps(as_numbers_dict)], shell=false, stderr=true, stdin=subprocess.pipe, stdout=subprocess.pipe) proc[router].stdin.write(bytes(router_status[router], encoding='utf-8') + b'\n')
and child process
router_status[router]=sys.stdin.readline().strip() path = os.path.expanduser('~' + '/bgp_routers/' + router) open(path + '/router_status.txt', 'w') f: f.write(router_status[router])
but not work! , pass second string router_status[router]='off' process proc[router].stdin.write(bytes(router_status[router], encoding='utf-8')
proc[router].stdin.flush()
and still not anything!
ok, not familiar arguments passing subprocess.popen() unable check whether correct. know thing or 2 subprocess module , there things consider looking @ code:
- using
shell=false
not need defined explicitly standard setting (although, if want make explicit reason, allright of course). - the argument
stderr=false
incorrect. must eithernone
orsubprocess.pipe
.
then, since using python3.x docs state that:
warning - use communicate() rather .stdin.write, .stdout.read or .stderr.read avoid deadlocks due of other os pipe buffers filling , blocking child process.
so you'd better of using proc[router].communicate(input="your standard input, meaning stdin")
it states:
popen.stdin - if stdin argument pipe, attribute writeable stream object returned open(). if encoding or errors arguments specified or universal_newlines argument true, stream text stream, otherwise byte stream. if stdin argument not pipe, attribute none.
in case means stdin should indeed byte stream.
overal, guessing should like:
proc[router] = subprocess.popen([sys.executable, os.getcwd() + '/' + router + '.py', router, json.dumps(graph), json.dumps(as_numbers_dict)], stderr=subprocess.pipe, stdin=subprocess.pipe, stdout=subprocess.pipe) proc[router].stdin.communicate(str(router_status[router]).encode())
i still wondering however, why did include:
path = os.path.expanduser('~' + '/bgp_routers/' + router) open(path + '/router_status.txt', 'w') f: f.write(router_status[router])
since unrelated subprocess.popen() statement. write user input textfile, capture latest input btw. if you'd save user input change 'w'
'a'
(this put file in append-mode instead of write-mode).
Comments
Post a Comment