简体   繁体   English

动态Python用户输入到单独的C程序

[英]Dynamic python user input to a seperate C program

I have a python GUI interface written in Tkinter. 我有一个用Tkinter编写的python GUI界面。 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. 该信息将在Python GUI中输入,并且处理图像修改的C程序需要此信息作为输入。 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 由于图像可能很大,因此我想尝试避免始终通过诸如以下的调用来重新运行C程序(这涉及内存分配,读取巨型文件等)。

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. 我希望有一个系统,一旦我调用my_C_Program ,它将所有资源加载到内存中后就可以处于等待状态。 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 . 我当时在想涉及getchar()东西,但是我不知道如何从python获取输出以转到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. 我已经在SO上看到了一些类似的问题,但是我无法完全确定这些方案将如何专门帮助挖掘。

If getchar() is the answer, can someone please explain how stdout works with multiple terminals open? 如果getchar()是答案,有人可以解释一下在打开多个终端的情况下stdout的工作方式吗?

As well, I'm trying to keep this program easily multiplatform across linux/mac/windows. 同样,我试图使该程序在linux / mac / windows上轻松保持多平台。

To summarize, I want the following functionality: 总而言之,我想要以下功能:

  1. User selects certain input from python GUI 用户从python GUI中选择某些输入
  2. That input becomes the input for a C program 该输入成为C程序的输入
  3. That C program can handle more input without having to be run again from the start (avoiding file I/O, etc). 该C程序可以处理更多输入,而不必从头开始运行(避免文件I / O等)。

The first thing you should probably do is start using Python's subprocess module , rather than os.system . 您可能应该做的第一件事是开始使用Python的subprocess os.system 模块 ,而不是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 . 完成此操作后,就可以对其进行更改,以便可以在Python中编写C程序的stdin ,而不是继承Python脚本的stdin

After that, you could just have Python send data over that the C program can interpret. 之后,您可以让Python通过C程序可以解释的方式发送数据。 For example, you might want to use a bunch of JSON chunks, one per line, like Twitter's streaming API 1 ; 例如,您可能想使用一堆JSON块,每行一个,就像Twitter的流API 1一样 the Python script makes a request dictionary, serializes it with json.dump , and then writes a newline. Python脚本创建一个请求字典,使用json.dump对其进行序列化,然后编写换行符。 The C program reads a line, parses the JSON, and handles the request. C程序读取一行,解析JSON,然后处理请求。

1 Upon reading the documentation, it looks like their implementation is a little more complex. 1阅读文档后,看起来它们的实现有些复杂。 You could adopt how they do it or just do it like I described. 您可以采用他们的方式,也可以按照我的描述进行。

icktoofay and JasonFruit have suggested decent approaches; icktoofayJasonFruit提出了体面的方法。 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. 如果将C程序编写为在TCP套接字上侦听请求和答复的服务器 ,则可以更轻松地更改客户端,使其支持多个并发客户端,对客户端或服务器执行近乎无缝的升级,而无需修改另一种方法,或者您可以将C程序移至功能更强大的硬件,而无需做太多修改配置的事情。

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. 这已经足够好了,但是您可能更喜欢将数据编码为一些标准化的格式,例如JSONASN.1 ,这样可以节省一些手动的字符串处理。

Could you do something with pexpect ? 你可以做的东西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. 如果您使用的是Windows(正如我在评论中指出的那样),则可以尝试使用winpexpect ,这是相似的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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