[Twisted-Python] server to server leaking descriptors
john nielsen
jn at who.net
Fri Apr 23 07:47:36 MDT 2004
I'm trying to figure out server to server communication. Here is example code that works but it leaks
descriptors until you run out.
In this example, the client talks to server1 on port 7000, server1 talks to server2 on 7001, then the client gets the result from server2 on 7001. Server1 uses pb.PBClientFactory() to send data to server2, which is the part that leaks descriptors.
What is the appropriate way for a server to talk to a server?
Thanks for any info,
john
------------
from twisted.spread import pb
from twisted.internet import reactor, defer
from twisted.python import util
import time,random
def client(port,fun,react=None,data=None):
print 'in client',port,fun,data
a=[]
factory = pb.PBClientFactory()
reactor.connectTCP('localhost', port, factory)
d = factory.getRootObject()
d.addCallback(lambda object: object.callRemote(fun,data))
d.addCallback(lambda result: a.append(result))
d.addErrback(lambda reason: 'error: '+str(reason.value))
#d.addCallback(util.println)
if react:
d.addCallback(lambda _: reactor.stop())
reactor.run()
if a:
print 'returning',a[0]
return a[0]
#return 1
class adder(pb.Root):
Result=None
def remote_add(self,i):
print 'in add'
i+=1
print 'calling add2'
client(7001,'add2',react=None,data=i)
def remote_add2(self,i):
print 'in add2',self.Result
i+=1
self.Result=i
def remote_result(self,stuff): return self.Result
port=raw_input('port?').strip()
if port:
port=int(port)
print 'starting on',port
reactor.listenTCP(port, pb.PBServerFactory(adder()))
reactor.run()
else:
client(7000,'add',react=None,data=10)
print 'got',client(7001,'result',react=1)
--
___________________________________________________________
Sign-up for Ads Free at Mail.com
http://promo.mail.com/adsfreejump.htm
More information about the Twisted-Python
mailing list