Python Forum
using subpocess for both reading and writing simultaneously
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using subpocess for both reading and writing simultaneously
#1
Hello
I have a very simple python code cpt.py:
for i in xrange(2):
   print i                 # line 2
   ans = raw_input()       # line 3
   print ans               # line 4
I want to write another python program that run cpt.py,
read the first item (printed by line 2) echo that value
which will be read by line 3, read the new line (line 4)
an do it twice since cpt.py loop 2 times.

I tried without success (since I am asking !)
cmd = "cpt.py"
p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE)
p.stdout.readline(ans) # read the print i (line 2)
p.stdin.writelines(ans)# write it (line3)
p.stdout.readline(ans) # read the print a (line 3)
p.stdout.readline(ans) # read  (line 2) second time
p.stdin.writelines(ans)# write it(line 3) second time
p.stdout.readline(ans) # read (ligne 3) second time
Thank you very much

JC
Reply
#2
For starters, you did not start your process right.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
[python]
for i in xrange[2):

[\python]
Reply
#4
Put the code in a function. The second program imports cpt and runs a function.
Reply
#5
(May-29-2018, 06:16 PM)woooee Wrote: Put the code in a function. The second program imports cpt and runs a function that inputs the value and returns it to the calling program. Obviously you can do this a 2nd time for the 2nd input.

It does not import - it tries to run another program as an external process (and does not do it right in the process - pun intended Tongue ).

Please, try to understand the nature of the question before providing feedback
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#6
I think the problem is that 'cpt.py' is not an executable, so Popen fails in the execution.
As you have also redirected the stderr to a pipe you cannot see the error message that shall looks like "cpt.py: Not an executable"

In linux/mac the trick is as easy as adding the next header to cpt.py:
#!/usr/bin/env python3
# The previous line must be the 1st line, no spaces before the #.
And set the executable flag from the command line:
Output:
$> chmod +x cpt.py
For windows, refer to the official faq.

Other option is to run the script calling the python executable with something like:
cmd = "cpt.py"
with subprocess.Popen(['python', cmd], stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE) as p:
    txt = p.stdout.readline()  # read the print i (line 2)
    p.stdin.writelines(txt)    # write it (line3)
    a = p.stdout.readline()    # read the print a (line 3)
    b = p.stdout.readline(ans) # read  (line 2) second time
    p.stdin.writelines(b)      # write it(line 3) second time
    c = p.stdout.readline()    # read (ligne 3) second time
Reply
#7
(May-30-2018, 08:08 AM)volcano63 Wrote:
(May-29-2018, 06:16 PM)woooee Wrote: Put the code in a function. The second program imports cpt and runs a function that inputs the value and returns it to the calling program. Obviously you can do this a 2nd time for the 2nd input.

It does not import - it tries to run another program as an external process (and does not do it right in the process - pun intended Tongue ).

Please, try to understand the nature of the question before providing feedback
Please try to understand the answer before criticizing. Do you want help or not?
Reply
#8
(May-30-2018, 03:15 PM)woooee Wrote: Please try to understand the answer before criticizing. Do you want help or not?

I was not asking for help Snooty. And your second answer is as clueless as the first one. Have you ever used subprocess module, dear?
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#9
(May-30-2018, 09:31 AM)killerrex Wrote: I think the problem is that 'cpt.py' is not an executable, so Popen fails in the execution.
As you have also redirected the stderr to a pipe you cannot see the error message that shall looks like "cpt.py: Not an executable"

stderr definition is for the process itself - not for subprocess.Popen. The script did not start, among other reasons (let's assume that it has execution flag set and shebang line added, as per your advice):
  1. Path to the app was not set - like ./cpt.py
  2. Command string as parameter may only be provided when it is coupled with shell=True; with default value False list is required (as you have shown)
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#10
(May-30-2018, 08:11 PM)volcano63 Wrote: Command string as parameter may only be provided when it is coupled with shell=True; with default value False list is required (as you have shown)

I try not to use the shell=True mode... some bad experiences with the shell expansions.
I have done some additional tests and you are right that when you want to pass parameters with shell=False you need to pass them in a list. Is logical as Popen will try to blindly search for a file with the full name:
>>>with subprocess.Popen(['ls'], stdout=subprocess.PIPE) as p:
>>>    print(''.join(b.decode() for b in p.stdout.readlines()))
cpt.py
subp.py
>>> with subprocess.Popen('ls', stdout=subprocess.PIPE) as p:
>>>    print(''.join(b.decode() for b in p.stdout.readlines()))
cpt.py
subp.py
>>> with subprocess.Popen(['ls', '-r'], stdout=subprocess.PIPE) as p:
>>>    print(''.join(b.decode() for b in p.stdout.readlines()))
subp.py
cpt.py
>>> with subprocess.Popen('ls -r', stdout=subprocess.PIPE) as p:
>>>    print(''.join(b.decode() for b in p.stdout.readlines()))
Traceback (most recent call last):
  File "subp.py", line 27, in <module>
    with subprocess.Popen('ls -r', stdout=subprocess.PIPE) as p:
  File "/usr/lib64/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/lib64/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ls -r': 'ls -r'
But for a python script that is in the same folder works in both ways as long as you set it executable.
If the script is not executable it produces an error like:
Error:
Traceback (most recent call last): File "subp.py", line 7, in <module> with subprocess.Popen(['./cpt.py'], stdout=subprocess.PIPE) as p: File "/usr/lib64/python3.6/subprocess.py", line 709, in __init__ restore_signals, start_new_session) File "/usr/lib64/python3.6/subprocess.py", line 1344, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) PermissionError: [Errno 13] Permission denied: './cpt.py'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fastest Way of Writing/Reading Data JamesA 1 2,179 Jul-27-2021, 03:52 PM
Last Post: Larz60+
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 2,581 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  How to run same process simultaneously exploit123 1 2,438 Sep-19-2019, 10:08 AM
Last Post: Gribouillis
  Reading and writing files JakeHoward4 1 1,806 Aug-07-2019, 06:22 PM
Last Post: Yoriz
  Problem with reading and writing to file. darktitan 2 2,282 Jul-20-2019, 06:06 PM
Last Post: darktitan
  Control 2 stepper motor simultaneously jihene 2 4,009 May-08-2019, 05:27 PM
Last Post: DeaD_EyE
  Moving with objects simultaneously kom2 1 3,008 Apr-20-2019, 07:12 PM
Last Post: SheeppOSU
  reading csv and writing csv chawas 2 2,860 Aug-23-2018, 09:28 AM
Last Post: chawas
  controlling multiple server simultaneously. caligola 3 3,598 May-11-2018, 05:44 PM
Last Post: wavic
  How to define two functions run simultaneously within a function? Alberto 4 4,019 Feb-06-2018, 10:08 PM
Last Post: Alberto

Forum Jump:

User Panel Messages

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