Fw: [Twisted-Python] Different behavior under Windows and Linux

W K wlad.karyagin at mail.ru
Wed Jan 2 07:18:51 EST 2008



-----Original Message-----
From: W K <wlad.karyagin at mail.ru>
To: twisted-python at twistedmatrix.com
Date: Wed, 02 Jan 2008 14:38:37 +0300
Subject: [Twisted-Python] Different behavior under Windos and Linux

> Hello all, 
> 
> i'll be short. The application was developed, which sends streams to different nodes using TCP (basic.LineReceiver and ClientFactory were used) It works perfectly under WindowsXP. However, under (K)Ubuntu the data is not sent immediately, but after approximately 20 secs delay! Does anyone encounter similar inconsistencies? How can one tune the parameters responsible for buffering?
> 
> Thanks 
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>

Here is the sample code

from twisted.internet.protocol import Protocol, Factory, ClientFactory
from twisted.protocols import basic
from twisted.internet import reactor, protocol
import logging
from threading import Event
import sys
import time
import thread

class LineConnector (basic.LineReceiver):
    
    def __init__(self, session):
        self.session = session

    def connectionMade(self):
        self.session.cnd.set()

    def sendMessage(self, line):
        logging.debug("Sending line " + line)
        self.sendLine(line)

    
    def lineReceived(self, line):
     	logging.debug("Received line " + line)
		
  
class ServerConnector (basic.LineReceiver):
    
    def __init__(self, session):
        self.session = session

    def connectionMade(self):
        pass

    def sendMessage(self, line):
        logging.debug("Sending line " + line)
        self.sendLine(line)

    
    def lineReceived(self, line):
     	logging.debug("Received line " + line)
################################################################################

class SimpleClient(ClientFactory):
    
    def __init__(self, host, port):
        
        self.port = port
        self.host = host
        
        self.connector = None
        self.cnd = Event()
		
        thread.start_new_thread(self.send, ())
        reactor.connectTCP(self.host, self.port, self)

            
  
		
		
    def send(self):
       while 1:
          self.cnd.wait()
          self.connector.sendMessage("TEST LINE")
          time.sleep(1)


    def buildProtocol(self, addr):
        logging.debug('ManagerOutput: Connected')
        
        self.connector = LineConnector(self)
        
        return self.connector

    def startedConnecting(self, connector):
        print 'Output: Started to connect.'
    
    def clientConnectionLost(self, connector, reason):
        print 'Output: Lost connection.'# Reason:', reason
     
        

    def clientConnectionFailed(self, connector, reason):
        logging.debug(self.name + ' output: Connection failed.')# Reason:', reason
        
################################################################################

class SimpleServer(protocol.ServerFactory):

    def __init__(self, port):
        self.port = port
        self.connector = None
        reactor.listenTCP(self.port, self)

    def buildProtocol(self, addr):
        logging.debug('Manager Connected')
        self.connector = ServerConnector(self)
        return self.connector
	
    def startedConnecting(self, connector):
        logging.debug('Started to connect')


    def clientConnectionLost(self, connector, reason):
        logging.debug('Lost connection. Reason:', reason)


    def clientConnectionFailed(self, connector, reason):
        logging.debug('Connection failed. Reason:', reason)


if __name__ == "__main__":
    
    logging.getLogger().setLevel(logging.DEBUG)
    logging.basicConfig(format='%(asctime)s %(levelname)8s %(message)s')#, filename='/tmp/logfile.txt', filemode='w')

    numargs = len(sys.argv) - 1

    if numargs == 1:
        print "Server started"
        manager = SimpleServer(31000)
    else:
        print "Client Started"
        mp = SimpleClient("127.0.0.1", 31000)
        
    
    reactor.run()





More information about the Twisted-Python mailing list