简体   繁体   中英

Dynamic python user input to a seperate C program

I have a python GUI interface written in Tkinter. The main point of the program is to setup many different variables for a final calculation on a hyperspectral image (geography stuff). Now, some further specifications have developed where the user would like to be able to actively input some parameters for groups of pixels to be smoothed. This information would be input in the Python GUI and the C programs that handle the image modifications need this as input. Since the images can be giant, I want to try and avoid always re-running the C program (which involves memory allocation, reading a giant file, etc.) with a call such as

os.system(./my_C_Program param1 param2 param3....)

I'd prefer to have a system where once I've called my_C_Program , it can be in a state of waiting after having loaded all the resources into memory. I was thinking something involving getchar() would be what I want, but I don't know how I can get the output from python to go my_C_Program . I've seen a few similar questions about this on SO, but I wasn't able to determine quite how those scenarios would help mine specifically.

If getchar() is the answer, can someone please explain how stdout works with multiple terminals open?

As well, I'm trying to keep this program easily multiplatform across linux/mac/windows.

To summarize, I want the following functionality:

  1. User selects certain input from python GUI
  2. That input becomes the input for a C program
  3. That C program can handle more input without having to be run again from the start (avoiding file I/O, etc).

The first thing you should probably do is start using Python's subprocess module , rather than os.system . Once you've done that, then you can change it so the C program's stdin is something you can write to in Python, rather than inheriting the Python script's stdin .

After that, you could just have Python send data over that the C program can interpret. For example, you might want to use a bunch of JSON chunks, one per line, like Twitter's streaming API 1 ; the Python script makes a request dictionary, serializes it with json.dump , and then writes a newline. The C program reads a line, parses the JSON, and handles the request.

1 Upon reading the documentation, it looks like their implementation is a little more complex. You could adopt how they do it or just do it like I described.

icktoofay and JasonFruit have suggested decent approaches; I'm going to suggest something to decouple the two programs a little further.

If you write your C program as a server that listens for requests and replies with answers on a TCP socket, you can more easily change clients, make it support multiple simultaneous clients, perform near-seamless upgrades of clients or servers without necessarily needing to modify the other, or you could move the C program to more powerful hardware without needing to do more than slightly modify configurations.

Once you have opened a listening socket and accepted a connection, the rest of your program could continue as if you were just interacting over standard input and standard output. This works well enough, but you might prefer to encode your data in some standardized format such as JSON or ASN.1 , which can save some manual string handling.

Could you do something with pexpect ? It lets you provide input to a command-line program, waiting for specified prompts from it before continuing. It also lets you read the intervening output, so you could respond to that as needed.

If you're on Windows (as I note from your comment that you are), you could try winpexpect , which is similar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM