twisted - locking threads when deferToThread is used

Ladislav Andel ladaan at iptel.org
Wed Aug 8 11:50:39 EDT 2007


Chris Mellon wrote:
> On 8/8/07, Chris Mellon <arkanes at gmail.com> wrote:
>   
>> On 8/8/07, Ladislav Andel <ladaan at iptel.org> wrote:
>>     
>>> Hi,
>>> I'm writing an application which will be periodically testing servers.
>>> I will have a global list of these servers(domain names) and need to do
>>> few tasks.
>>> 1) DNS checks - I will use asynchronous twisted-names for it
>>> - in case there is a difference comparing to the list it should update
>>> the list(then also in DB)
>>> 2) ICMP pings - should be also possible to do it asynchronously
>>> 3) Blocking function which will be pinging with SIP requests
>>> - here I will use function deferToThread to make it non-blocking.
>>>
>>> Questions:
>>> 1) How do I lock each thread when writing to a global list in twisted?
>>> 2) How will I put together all three results mentioned above in the
>>> global list
>>> - is it by using function callLater ?
>>> 3) Could you help me with a rough skeleton of this application, please?
>>>
>>> Thank you,
>>> Lada
>>>
>>>       
>> deferToThread returns the result of the passed function in a Deferred.
>> Callbacks and errbacks will be run in the main event loop, so you
>> don't need to worry about thread safety of the callbacks, just of the
>> function itself.
>>
>> The basic model would look something like this:
>>
>> def querySIP(what):
>>     return sip.query(what) #native, *threadsafe*, blocking call
>>
>> sipquery = reactor.deferToThread(querySIP, what)
>> sipquery.addCallback(lambda result: sip_results.append(result))
>> sipquery.addErrback(log.err)
>>
>>     
>
>
> And, of course, deferToThread is a function in the
> twisted.internet.threads module, not a method of the reactor.
>   
Thanks for your answer.

I have following code below.
There are few problems.

1) I need to call getServerResponse of SIP servers for given list 
domains periodically.
     I have tried to use callLater method but probably at wrong places.

2)
In my code is somthing wrong.
I get the response but also following error

Traceback (most recent call last):
  File 
"/home/data/iptel_summer_2007/sipcraft1/src/sipcraft/multideferred.py", 
line 46, in ?
    reactor.run()
  File "/usr/lib/python2.4/site-packages/twisted/internet/posixbase.py", 
line 220, in run
    self.mainLoop()
  File "/usr/lib/python2.4/site-packages/twisted/internet/posixbase.py", 
line 228, in mainLoop
    self.runUntilCurrent()
--- <exception caught here> ---
  File "/usr/lib/python2.4/site-packages/twisted/internet/base.py", line 
533, in runUntilCurrent
    f(*a, **kw)
  File "/usr/lib/python2.4/site-packages/twisted/internet/defer.py", 
line 239, in callback
    self._startRunCallbacks(result)
  File "/usr/lib/python2.4/site-packages/twisted/internet/defer.py", 
line 290, in _startRunCallbacks
    raise AlreadyCalledError
twisted.internet.defer.AlreadyCalledError:





More information about the Python-list mailing list