On Tue, May 5, 2009 at 6:39 AM, <[email protected]> wrote: > hey, > i'm quite new in python and twisted and did things wrong from the start now i > have to reconsider my prog. > first i thought of an easier example with almost the same functionality. > functionality: client connects, sends info, all infos from all clients are > concurrently processed, a special client with the right info at the right > time gets a special msg > i got this: > it is thought that a client writes an int as birthday back... > > from twisted.internet import protocol, reactor, defer, threads > days = [] > allprotocols = [] > class BServerProtocol(protocol.Protocol): > birthday = 0 > def connectionMade(self): > print "puh" > global allprotocols > allprotocols.append(self) > print "allprotocols",allprotocols > def congratulate(self): > self.transport.write("congratulations") > return 'done' > def dataReceived(self,data): > print data > if int (data)>0: > self.birthday = int(data) > print "self.bday:",self.birthday > global days > days.append(int(data)) > self.transport.write("pff") > # self.factory.deferred.callback("uiuiui+1") > if data == "thanks": > self.write("well done") > class BServerFactory(protocol.ServerFactory): > protocol = BServerProtocol > def __init__(self): > self.deferred = defer.Deferred() > def proveday(): > day = 5 #of may, think of this date as new decided > #from a white rabbit every time it runs > ret = -1 > found = False > i = 0 > length = len(allprotocols) > while (i<length and found == False): > birthday = allprotocols[i].birthday > print "birthday:",birthday > if birthday == day: > print 'bday==day' > print "allprotocols[i]:",allprotocols[i] > found =True > ret = i > allprotocols[i].congratulate() > i+=1 > return ret > def startprove(): > r = proveday() > > bsf = BServerFactory() > #bsf.deferred.addCallback(registerSuccess) > reactor.listenTCP(55555,bsf) > reactor.callInThread(startprove) > #commands =[(startprove,[],{})] > #commands.append(startcong)#,[],{}) > #threads.callMultipleInThread(commands) > reactor.run() > _______________________ > allprotocols[i].congratulate() should get in connection with the client > again, but nothing happens, when another client connects a lot of > congratulations are thrown, it seems transport buffers. i tried a lot of > things, with no better result, maybe i can get some hints to get a solution... > thx > stefan > >
One problem with the above code is you're calling transport.write from a thread: http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#WhydoesittakealongtimefordataIsendwithtransport.writetoarriveattheothersideoftheconnection The above code could also be improved by not using a thread and instead scheduling a call to check birthdays: http://twistedmatrix.com/documents/current/api/twisted.internet.task.LoopingCall.html It would also make sense to attach such functionality to the Factory rather than relying global state - maintain a list of protocols on the Factory instance. -Drew _______________________________________________ Twisted-Python mailing list [email protected] http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
