python 中的SimpleXMLRPCServer只能支持单线程访问对象.如果想使用多线程的 SimpleXMLRPCServer.最好的办法就是继承ThreadMixIn.简单吧:)下面是代码
服务器端代码 # -*- Python -*-
#
# server.py -
#
# Copyright (C) 2007 Leo Chen ()
#
# $Locker: $
# $Log: header.el,v $Revision 1.1 2001/02/01 20:15:57 lasse
# Author : Leo Chen
# Created On : Sat Jun 16 10:10:28 2007
# Last Modified By: Leo Chen
# Last Modified On: Sat Jun 16 10:10:38 2007
# Update Count : 1
#
# HISTORY
#
#-*- coding: cp936 -*-
import SimpleXMLRPCServer,SocketServer
import time,thread
#The server object
class Server:
count=0
def __init__(self):
pass
def ServeMe(self):
mutex.acquire() #用mutex锁住数据
Server.count+=1 #更改静态数据
t=time.strftime("Serve at %I:%M:%S Id is ")+str(Server.count)
print "Serve Id "+str(Server.count)
mutex.release()#释放锁
time.sleep(10)
return t
#多线程实现
class RPCThreading(SocketServer.ThreadingMixIn, SimpleXMLRPCServer.SimpleXMLRPCServer):
pass
global mutex
mutex =thread.allocate_lock()
server_object = Server()
server = RPCThreading(("localhost", 8888))
server.register_instance(server_object)
#Go into the main listener loop
print "Listening"
server.serve_forever()
客户端代码
# -*- Python -*-
#
# client.py -
#
# Copyright (C) 2007 Leo Chen ()
#
# $Locker: $
# $Log: header.el,v $Revision 1.1 2001/02/01 20:15:57 lasse
# Author : Leo Chen
# Created On : Sat Jun 16 10:10:56 2007
# Last Modified By: Leo Chen
# Last Modified On: Sat Jun 16 10:10:57 2007
# Update Count : 1
#
# HISTORY
#
import xmlrpclib
server = xmlrpclib.ServerProxy("")
id = server.ServeMe()
print id
注意.如果有共享数据.操作时要加锁.
阅读(3088) | 评论(0) | 转发(0) |