0

I'm looking for a way to create a Python program that wraps another Python program. This is what I mean:

while (otherProgram is waiting for raw_input):
 send some text to the other program

It is essential that I can do this in two separate programs.


Here is another example:

program1.py

text = raw_input()
print("You entered " + text)
text2 = raw_input()
print("Your second input was " + text2)
text3 = raw_input()
print("Your third input was " + text3)

program2.py

# use program1.py somehow
while program1_is_waiting_for_input:
 send_to_program1("prefix " + raw_input() + " suffix")

sample input into program2:

asdf
ghjkl
1234

sample output out of program2:

You entered prefix asdf suffix
Your second input was prefix ghjkl suffix
Your third input was prefix 1234 suffix



Things I have considered using:

  • I don't think eval or exec or anything like that would work, since I don't know how much code to execute.
  • The subprocess module might work if I pipe the outputs correctly, but can I "pause" the second program when waiting for input?
  • Maybe I should try to multithread the non-wrapper program, but I don't know how I would go about doing that
  • Download an open-source python interpreter and try to work off that (seems overly difficult for a relatively simple problem)



What I hope to end up with

I eventually want to end up with a Python program which, each time it is run, inserts another successive input into the wrapped program at the point it left off, and pipes the output into stdout. If it is easier to do this, that would be great.

asked Feb 10, 2014 at 10:45
5
  • 1
    I am not very sure, but you might want to look at gevent module. Commented Feb 10, 2014 at 10:47
  • 1
    Why can't you import components from that other script? Commented Feb 10, 2014 at 10:51
  • What do you mean by "pausing" the second program? Commented Feb 10, 2014 at 11:16
  • @Blender I can't be sure of the format of the script, or the order of the code that is executed. I need to specifically pipe through input in the order specified by the raw_input()s in the wrapped program Commented Feb 10, 2014 at 11:48
  • @JanneKarila I mean that the wrapped program stops its execution and waits for input from the wrapper program to continue. Commented Feb 10, 2014 at 11:49

2 Answers 2

1

Here is an example of running the child process using subprocess.

import subprocess
program1 = subprocess.Popen("program1.py", stdin=subprocess.PIPE, stdout=subprocess.PIPE)
while program1.poll() == None:
 cmd_to_send = "prefix " + raw_input() + " suffix"
 program1.stdin.write(cmd_to_send + "\n")

Your child process will wait for input because raw_input is a blocking call until it receives a line of input.

answered Feb 10, 2014 at 11:10
Sign up to request clarification or add additional context in comments.

Comments

1

take a look at subprocess module or https://pypi.python.org/pypi/pexpect-u/

answered Feb 10, 2014 at 10:50

1 Comment

pexpect was what I was looking for. gevent looks good for the successive inputs idea

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.