简体   繁体   English

具有后台进程的命令行界面应用程序

[英]Command line interface application with background process

I'm new to python. 我是python的新手。 I'm trying to write an application with command line interface. 我正在尝试使用命令行界面编写应用程序。 The main application is communicating with server using tcp protocol. 主要应用程序正在使用tcp协议与服务器通信。 I want it to work in the background so I won't have to connect with the server every time I use interface. 我希望它在后台运行,这样我每次使用界面时都不必与服务器连接。 What is a proper approach to such a problem? 解决此问题的正确方法是什么?

I don't want the interface to be an infinite loop. 我不希望接口成为无限循环。 I would like to use it like this: my_app.py command arguments. 我想这样使用它:my_app.py命令参数。

Please note that I have no problems with writing interface (I'm using argparse library right now) but don't know what architecture would suit me best and how to implement it in python. 请注意,编写接口没有问题(我现在正在使用argparse库),但不知道哪种架构最适合我以及如何在python中实现它。

If you put something in the background, then it's no longer connected to the current shell (or the terminal). 如果在后台放置某些内容,则该内容将不再连接到当前外壳程序(或终端)。 So you would need the background process to open a socket so the command line part could send it the command. 因此,您需要后台进程来打开套接字,以便命令行部分可以向其发送命令。

In the end, there is no way around creating a new connection to the server every time you start the command line process and close the connection when the command line process exits. 最后,每次启动命令行过程并在退出命令行过程时关闭连接时,都无法绕开与服务器的连接。

The only alternative is to use the readline module to simulate the command line inside of your script. 唯一的选择是使用readline模块在脚本内部模拟命令行。 That way, you can open the connection, use readline to ask for any number of commands to send to the server. 这样,您可以打开连接,使用readline询问要发送到服务器的任意数量的命令。 Plus you need an "exit" command which terminates the command line process (which also closes the server connection). 另外,您需要一个“退出”命令来终止命令行过程(这也会关闭服务器连接)。

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

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