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.
As part of my experimentation and goal to gain knowledge of python and its available libraries; 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. The Varnish application is an example of a tool that offers similar administrative functionality.
My knowledge of threads is limited, 开发者_如何学Cand 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".
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?
Alternatively, a list of python applications that offer such a feature would be a great help. I'd gladly poke through code, in order to reuse their ideas.
python includes some multi-threading servers (SocketServer
, BaseHTTPServer
, xmlrpclib
). You might want to look at Twisted as well, it is a powerful framework for networking.
Probably the easiest starting point would involve Python's xmlrpclib.
Regarding threading, all threads can read all data in a Python program; 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 (i.e. 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; 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.
multiprocessing is a package that supports spawning processes using an API similar to the threading module. 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.
The GIL is controversial because it prevents multithreaded CPython programs from taking full advantage of multiprocessor systems in certain situations.
精彩评论