Search code examples
pythonsocketsnetwork-programmingclient-servertwisted

Getting Twisted to work in the background


Hi i new with Twisted and i want to use it as my client/server upgrade.

I have a software that do some unit testings, and i want to add Twisted.

There are 2 computers 1 for controlling tests (Client) and 1 for testing specific Unit(Server)

With the server part there is no problem using twisted. since when activating the reactor the server is listening and waiting for requests from the client.

at the client part i have some problems.

when staring the client

reactor.run()

the software entering to event driven mode and wait for event... (connection, send....) the problem is that i want to do a lot of things and not just watching my communication.

Lets say i have a function:

voltage = self.UnitTest.GetVoltage()

if voltage........

current = self.UnitTest.GetCurrent()

if current.....

I want the UnitTest methods to send their request threw twisted client, is it possible?


Solution

  • If it's for testing , have a look to the documentation: unit test with trial

    to write your test you can do on the classic way of callback:

    from twisted.internet import defer
    
    class SomeWarningsTests(TestCase):
        def setUp(self):
            #init your connection , return the deferred that callback when is ready
        def tearDown(self):
            # disconnect from the server
        def getVoltage(self):
            #connect to you serveur and get the voltage , so return deferred
        def test_voltage(self):
            def testingVoltage(result):
                 if not result:
                      raise Exception("this not normal")
                 return result
            return self.getVoltage.addCallback(testingVoltage)
    
        #other way
        def getCurrent(self):
            #connect to you serveur and get the current(?) , so return deferred
    
        @defer.inlineCallbacks
        def test_current(self):
            current = yield self.getCurrent()
            if not current:
                raise  Exception("this not normal")
            defer.returnValue(current)