[Twisted-Python] Connect to a Twisted Perspective Broker from a VB application (using a python com server InProc)
Hermínio Miguel Sanches
hsanches at gmail.com
Wed Aug 23 09:32:53 EDT 2006
Hi,
I need to connect to a PerspectiveBroker from a VB application.
I did it with a com server in python, which works fine when the com client
is also python code.
All the calls must be synchronous to the com server.
When Calling from VB it works fine every time if the function used from the
com server don't use the reactor.
The problem is that when I call (from the VB com client) the functions that
uses the twisted reactor it performs well the first time but for the the
second time it's called, it stalls. I have to kill the vb executable.
The function from the test code is: testBroker()
Please can you help me to sheed some light on this? Should I do it another
way? Bellow is the code that I am using.
Many thanks
Miguel
----------------------------------------------------
The code for the python com client :
import win32com.client
object = win32com.client.Dispatch("Python.TestServer")
print object.Hello("test")
print object.Hello2("test")
print object.testBroker()
print object.gettestresult()
----------------------------------------------------
The code for the VB com client:
Private Sub TestComServer()
Dim y As Object
Set y = CreateObject("Python.TestServer")
MsgBox y.gettestresult
MsgBox y.testBroker()
MsgBox y.gettestresult()
Set y = Nothing
End Sub
----------------------------------------------------
the code for the python comserver:
#testcom.py
from twisted.spread import pb
from twisted.internet import reactor, defer
from twisted.python import util
import sys
import time
import socket
class HelloWorld:
_reg_clsid_ = "{7CC9F362-486D-11D1-BB48-0000E838A65F}"
_reg_desc_ = "Python Test COM Server"
_reg_progid_ = "Python.TestServer"
_public_methods_ = ['testBroker','gettestresult']
def __init__(self):
self.sText='sText10'
self.reactorFinished=False
def get_ip(self):
addrs = socket.getaddrinfo(socket.gethostname(), None)
addr = addrs[0]
IPaddr = addr[4][0]
return IPaddr #"localhost"
def get_ip(self):
return "localhost"
def get_port(self):
return 8786
def DealWithReactor(self):
now = time.localtime(time.time())
factory = pb.PBClientFactory()
reactor.connectTCP(self.get_ip(), self.get_port(), factory)
d = factory.getRootObject()
strt="hello network " + ' ' + time.asctime(now)
d.addCallback(lambda object: object.callRemote("echo", strt))
d.addErrback(lambda reason: 'error: '+str(reason.value))
d.addCallback(self.StopReactor)
reactor.run()
def StopReactor(self,obj):
reactor.stop()
self.reactorFinished=True
self.sText=str(obj)
return obj
def testBroker(self):
self.reactorFinished=False
self.DealWithReactor()
while self.reactorFinished==False:
pass
return 'OK testBroker22 self.reactorFinished:' + str(
self.reactorFinished)
def gettestresult(self):
return "gettestresult:" + str(self.sText)
if __name__=='__main__':
import win32com.server.register
win32com.server.register.UseCommandLine(HelloWorld)
h=HelloWorld()
print(h.gettestresult())
print(h.testBroker())
print(h.gettestresult())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20060823/485bc1ff/attachment.htm
More information about the Twisted-Python
mailing list