[Twisted-Python] again deferToThreads

Ladislav Andel ladaan at iptel.org
Thu Aug 16 08:52:49 EDT 2007


Hello,
I have a two functions which I run in one thread called via deferToThread.
There is function called siptest_f and function pingNode test.
siptest_f returns a dictionary and pingNode returns the average time out 
of 3 received packets.
Below is code I use. I run it through reactor event loop which is not 
shown here.
My problem is that the function on its own returns correct results but 
if use it within following code
I get always different results and mostly wrong.
There should probably be some way of locking but I'm not sure how to do 
it correctly.
If you have any other suggestions, there are welcomed.

Lada

------------------------------------------------------------------------

def serverTest(dns_res, index):
    """ Function for SIP testing """
   
    from twisted.internet import threads
    from siptest import siptest_f
    from icmp_ping2 import pingNode
#    import thread
   
   
    print "here will be siptest for ", dns_res
    #stdoutmutex = thread.allocate_lock()

    def test(dns_res, test_opts):
        if dns_res:
            icmpres =  pingNode(alive=0, timeout=1.0, ipv6=0, number=3, 
node=dns_res, flood=0, size=56)
            sipres = siptest_f(dns_res, test_opts)
       
           # stdoutmutex.acquire()  
            sipres['ICMP'] = icmpres
           # stdoutmutex.release()
           
            return sipres
        raise ValueError("Error occured")   
   
    def sipResult(d):

        print d
        #return d

    def printError(failure):
        print failure

    sd = threads.deferToThread(test, dns_res, test_opts)

    sd.addCallback(sipResult)
    sd.addErrback(printError)





More information about the Twisted-Python mailing list