Python Forum
trying out subprocess in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying out subprocess in python
#1
Folks,
I'm trying to checkout the sub-process functionality of Python and am having some trouble, specifically on the subprocess accepting stdin...

here is my masterPrint.py

import subprocess
#subprocess.call(["python", "slavePrint.py"], shell=True)
#stdout of subprocess is piped to p
#stdin of subprocess is piped to p
p = subprocess.Popen(["python", "slavePrint.py"], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
while(1):
#read in string
strng = input("enter a number between 1 and 10")
str = p.communicate(str(strng)+ '\n')[0]
print str
here is my slavePrint.py

 a = input()
print ('slave got ' + str(a))
Here is the error I am getting when I run the code:

C:\Users\oahmad>py -2 masterPrint.py
enter a number between 1 and 102
Traceback (most recent call last):
File "slavePrint.py", line 1, in <module>
a = input()
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
enter a number between 1 and 10
Reply
#2
Use raw_input() instead input(). Python 2?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
issue was using str, which is a keyword in python...
Reply
#4
(Jun-13-2017, 12:01 AM)sblu23 Wrote: issue was using str, which is a keyword in python...
I disagree. If that was the problem, you would have had a different error. This looks more like you're using input() with python2, which calls eval() on whatever you pass (and it thus a terrible thing to use).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python loop in subprocess vinothkumargomu 6 3,404 Jul-06-2020, 12:02 PM
Last Post: vinothkumargomu
  Python Error in subprocess.py roydianton90 1 5,702 Dec-14-2018, 11:26 AM
Last Post: jeanMichelBain
  OS command via python subprocess module alinaveed786 21 11,500 Oct-23-2018, 05:40 AM
Last Post: alinaveed786
  subprocess in thread python to check reachability anna 3 3,566 Sep-05-2018, 04:01 PM
Last Post: woooee
  Using Subprocess.Popen to start another python script running in background on Window johnb546 0 13,805 Jun-01-2018, 01:57 PM
Last Post: johnb546
  proc communicate not exiting on python subprocess timeout leoonardoo 0 3,893 Sep-13-2017, 09:54 AM
Last Post: leoonardoo
  I have problem on Communication between C++ and Python using subprocess Module hlfan1234567 0 4,319 Jul-09-2017, 04:34 PM
Last Post: hlfan1234567
  Encoding issue while running Sysinternals Autorunsc via Python subprocess JChris 0 3,517 Jun-13-2017, 12:01 AM
Last Post: JChris

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020