[Twisted-Python] Question on deferreds
Frank Millman
frank at chagford.com
Fri Dec 9 04:54:09 EST 2005
Hi Ken
Many thanks for your reply. Sorry for not replying earlier - I seem to have
lost some mail due to ISP problems, and I just noticed your message in the
archive.
I have not included your response, as I want to come at this from another
angle. I confess that I am under pressure to 'get something working', even
if it is not the optimal solution, and this is all getting a bit
complicated.
I have been experimenting with using a socket client instead of Twisted, and
I got something up and running quite quickly. When I take a step back and
look at how I got it working, I feel that I should be able to apply the same
technique to Twisted, but I cannot get it to work. I will show both methods,
and perhaps someone can point me in the right direction.
The trick is that I have two threads running concurrently - a wxPython main
loop, and a subthread that monitors the socket. The socket loop looks like
this (simplified), subclassed from threading.Thread -
def run(self):
readable = [s.fileno()]
error = []
self.sendData = []
while 1:
if self.sendData:
writable = [s.fileno()]
else:
writable = []
r,w,e = select.select(readable,writable,error,0.01)
if r:
self.recvData = s.recv(1024)
if w:
s.send(self.sendData.pop(0))
def checkData(self,item,value): # this is called from the *wx* thread
self.recvData = None
self.sendData.append(cPickle.dumps((CHECK,item,value)))
while self.recvData is None:
sleep(0.01)
return self.recvData
As you can see, checkData() blocks the wxPython thread until it receives a
response from the socket thread.
This is how I tried with Twisted -
def checkData(self,item,value): # this is called from the *wx* thread
self.recvData = None
self.callCheckData(item,value)
while self.recvData is None:
sleep(0.01)
return self.recvData
def callCheckData(self,item,value):
self.avatar.callRemote('checkData',item,value).addCallback(self.dataChecked)
def dataChecked(self,answer):
self.recvData = answer
It looks as if it should work, but the server method 'perspective_checkData'
never gets called.
Is it possible to do anything like this?
Thanks
Frank Millman
More information about the Twisted-Python
mailing list