简体   繁体   English

开发基于线程tcp的管理界面的建议

[英]Suggestions for developing a threaded tcp based admin interface

I've built a very simple TCP server (in python) that when queried, returns various system level statistics of the host OS running said script. 我构建了一个非常简单的TCP服务器(使用python),该服务器在查询时返回运行所述脚本的主机OS的各种系统级统计信息。

As part of my experimentation and goal to gain knowledge of python and its available libraries; 作为我实验和目标的一部分,以获取有关python及其可用库的知识; i would like to build on an administration interface that a) binds to a separate TCP socket b) accepts remote connections from the LAN and c) allows the connected user to issue various commands. 我想建立在一个管理界面上:a)绑定到单独的TCP套接字b)接受来自LAN的远程连接,并且c)允许连接的用户发出各种命令。 The Varnish application is an example of a tool that offers similar administrative functionality. Varnish应用程序是提供类似管理功能的工具的示例。

My knowledge of threads is limited, and I am looking for pointers on how to accomplish something similar to the following : 我对线程的了解有限,我正在寻找有关如何完成类似于以下内容的指示:

user connects to admin port (telnet remote.host 12111), and issues "SET LOGGING DEBUG", or "STOP SERVICE". 用户连接到管理端口(telnet remote.host 12111),并发出“ SET LOGGING DEBUG”或“ STOP SERVICE”。

My confusion relates to how i would go about sharing data between threads. 我的困惑与我如何在线程之间共享数据有关。 If the service is started on for example thread-1 , how can i access data from that thread? 如果该服务是在例如thread-1上启动的,我如何从该线程访问数据?

Alternatively, a list of python applications that offer such a feature would be a great help. 另外,提供此类功能的python应用程序列表也会有很大帮助。 I'd gladly poke through code, in order to reuse their ideas. 为了重用他们的想法,我很乐意浏览代码。

python includes some multi-threading servers ( SocketServer , BaseHTTPServer , xmlrpclib ). python包括一些多线程服务器( SocketServerBaseHTTPServerxmlrpclib )。 You might want to look at Twisted as well, it is a powerful framework for networking. 您可能还想看看Twisted,它是一个强大的网络框架。

Probably the easiest starting point would involve Python's xmlrpclib. 最简单的起点可能涉及Python的xmlrpclib。

Regarding threading, all threads can read all data in a Python program; 关于线程,所有线程都可以读取Python程序中的所有数据。 only one thread at a time can modify any given object, so primitives such as lists and dicts will always be in a consistent state. 一次只能有一个线程可以修改任何给定的对象,因此诸如列表和字典之类的基元将始终处于一致状态。 Data structures (ie class objects) involving multiple primitives will require a little more care. 涉及多个原语的数据结构(即类对象)将需要多加注意。 The safest way to coordinate between threads is to pass messages/commands between threads via something like Queue.Queue; 在线程之间进行协调的最安全方法是通过Queue.Queue之类的方法在线程之间传递消息/命令。 this isn't always the most efficient way but it's far less prone to problems. 这并不总是最有效的方法,但是它不容易出现问题。

Best use the multiprocessing library, it provides a full set of functionality for parallel computing (Queues, Pipes, ...). 最好使用多重处理库,它提供了用于并行计算的完整功能集(队列,管道等)。 Multithreading in python is not efficient due to the limitations that come with the GIL . 由于GIL的限制,python中的多线程效率不高。

multiprocessing is a package that supports spawning processes using an API similar to the threading module. multiprocessing是一个程序包,它使用类似于线程模块的API支持生成过程。 The multiprocessing package offers both local and remote concurrency, effectively side-stepping the Global Interpreter Lock by using subprocesses instead of threads. 多处理程序包提供本地和远程并发,通过使用子进程而不是线程来有效地避开全局解释器锁。 Due to this, the multiprocessing module allows the programmer to fully leverage multiple processors on a given machine. 因此,多处理模块允许程序员充分利用给定机器上的多个处理器。 It runs on both Unix and Windows. 它可以在Unix和Windows上运行。

The GIL is controversial because it prevents multithreaded CPython programs from taking full advantage of multiprocessor systems in certain situations. GIL之所以引起争议,是因为它在某些情况下阻止多线程CPython程序充分利用多处理器系统。

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

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