Implementation of Python multi-thread XML-RPC

Source: Internet
Author: User

Implementation of Python multi-thread XML-RPC I. IntroductionThe XML-RPC server implemented with simplexmlrpcserver of python can only be a single thread, processing requests one by one, for the operation of time-consuming operations will block other user requests, the quality of service is reduced, so is there a way to make simplexmlrpcserver A multithreaded server? There are two main implementation methods: Modify the implementation code of simplexmlrpcserver and use threadingmixin or forkingmixin. Ii. Modify the implementation code of simplexmlrpcserverThe implementation of simplexmlrpcserver is as follows: Class simplexmlrpcserver (socketserver. tcpserver, simplexmlrpcdispatcher): If we inherit the socketserver. tcpserver, instead of threadingtcpserver that supports thread processing, you can change simplexmlrpcserver to a multi-thread server, as shown below: Class simplexmlrpcserver (socketserver. threadingtcpserver, simplexmlrpcdispatcher ): 3. threadingmixin implementationThe socketserver module supports threading or fork. The typical code is as follows:
Import simplexmlrpcserver, socketserver import WIN32API, thread # The Server Object Class server: Count = 0 def _ init _ (Self): Pass def echostring (self, STRs): mutex. acquire () # Use mutex to lock the data server. count + = 1 # change static data t = time. strftime ("serve at % I: % m: % s ID is") + STR (server. count) print "serve ID" + STR (server. count) mutex. release () # Release the lock # WIN32API. sleep (20000) return T # multi-threaded implementation class rpcthreading (socketserver. threadingmixin, simplexmlrpcserver. simplexmlrpcserver): Pass global mutex = thread. allocate_lock () server_object = server () Server = rpcthreading ("localhost", 8888) server. register_instance (server_object) # go into the main listener loop print "listening on 8888" server. serve_forever ()
Through the above Code can also achieve multi-threaded XML-RPC support without modifying Python simplexmlrpcserver library.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.