1

I'm new to Stack Overflow. I have a genetic algorithm written in C that accepts user input in the form of a number 0-100, and outputs an array of numbers. The C code is a full, stand-alone compiled program. It has a command-line interface. I'm relatively new to programming, mostly hacking until I find a solution to a specific task. and I'm very confused in reading the Python Subprocess management documentation. I have a GUI written in Python using tkinter, and I have a box where the user can type their response value (0-100). I also have an empty array in my code that I want to populate with the output from the genetic algorithm. The user will use that array for something, give another response (0-100) the C code will take that response, produce another array of numbers, and the process continues. My question is, can anyone explain to this novice in simple terms how to use the subprocess module to link my python GUI and the C code genetic algorithm together to this end? Thank you!

1
  • here's an example of interaction with a subprocess: quickdraw.py -- it wraps java subprocess to provide Python API. Commented Nov 25, 2013 at 1:51

1 Answer 1

0

I assume that you are able to store the text that the user enters in a variable? If not, this question explains it pretty nicely. Anyway, once you get that, call subprocess.check_output like this:

 result = subprocess.check_output(["./cexecutable", inputValue])

(replace "cexecutable" with the name of the executable of your genetic algorithm program and inputValue with whatever variable you're storing the input in)

This should store the output of the genetic algorithm in result. It will be all one string, so if there are multiple lines of output you'll probably want to call result.split("/n") to get a list of lines. You can then parse them and put them into the array as you see fit, based on how they're formatted.

Assuming that you have some sort of "enter" button associated with the text box, and you're doing this all as an event that occurs when the button is clicked, this will happen every time the user enters new text and clicks the button.

EDIT (in response to comment): To keep the program running in the background, you'll need to use subprocess.Popen and Popen.communicate. Rather than just returning the output of your program, this will create a Popen object that you can continue to interact with. Without testing this on your code, I can't guarantee that the code below will do exactly what you want, but I think it should be close:

 genAlg = subprocess.Popen(["./executable"])
 ...
 #user enters data
 ...
 result = genAlg.communicate(inputValue)[0] 
 #communicate sends the given argument to stdin, and returns a 
 #tuple (stdout, stderr) - you want stdout, hence the "[0]"

EDIT 2: Turns out that's not actually a workable solution - see J.F. Sebastian's comments below.

Sign up to request clarification or add additional context in comments.

13 Comments

Thank you for the thorough explanation, I really appreciate it! I am able to save the text that the user inputs as a variable, so the way you've described it doesn't sound as scary as the documentation made it seem to me. I think the genetic algorithm, once opened, should stay open and running in the background. It's job is to get feedback from the user as to the utility of its output. The algorithm and the user are working together to find a particular sequence, its not simply a calculator with inputs and outputs. Would your method work in keeping the C code running in the background?
Oh, I see, that will be more complicated, but should still be doable. If you're running the genetic algorithm outside of of python, how does it expect to receive the feedback? Do you just type it on the command line (i.e. to the stdin stream)?
Yep it's a command line interface. The algorithm does some magic, produces an array of numbers, the user uses that array for something (not important in the scope of this question) gives some feedback (0-100) as to the utility of those numbers on the command line, then the algorithm uses that feedback to adjust the space it is exploring, and outputs another array of numbers. So it might take the algorithm and the user 10,000 trials before a good array of numbers is found. I just wrote the GUI to make it less scary-looking for the user. I hope I'm describing it in enough detail, thanks!
I could also adjust the C code output to write to a .csv file or something, and have the python code read the file. That wouldn't take care of the problem of getting the user input into the C program though. I'm not sure what the best plan of attack is, I just hoped there was a way to use the C program with a python GUI wrapper.
The .csv idea is creative, but it shouldn't be necessary. I edited my answer with a more streamlined solution.
|

Your Answer

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.