[Twisted-Python] questions about twisted usage

Orestis Markou orestis at orestis.gr
Thu Mar 22 08:40:04 EDT 2012


On 22 Μαρ 2012, at 12:34 μ.μ., Uri Okrent wrote:

> On Thu, Mar 22, 2012 at 2:17 PM, Itamar Turner-Trauring
> <itamar at itamarst.org> wrote:
>> On 03/22/2012 08:05 AM, Uri Okrent wrote:
>>> This implies that twisted can (and will) switch contexts from one
>>> deferred to another even in the middle of execution of the first
>>> deferred, correct?
>> 
>> No. Deferreds are just slightly nicer callbacks. They have nothing to do
>> with the event loop, or scheduling, or context switching.
> 
> Maybe I should clarify my question:
> 
> thing = 0
> def a():
>    thing += 1
>    thing += 1
>    thing += 1
>    thing += 1
>    thing += 1
> 
> def b():
>    thing = 0
> 
> def show_thing():
>    print thing
> 
> a.addCallback(show_thing)
> b.addCallback(show_thing)
> 
> given the two deferreds 'a' and 'b', with 'a' being called first, it
> is possible for twisted to jump to 'b' in the middle of 'a' correct?
> I.e., when show_thing runs after 'b' is finished it will always print
> 0 because thing was set to 0 by 'b', but when show_thing runs as the
> callback for 'a', it will print something between 0 and 5 and it is
> non-deterministic, because 'b' may have jumped in at some point during
> the execution of 'a' and reset 'thing' back to 0.

No, Twisted runs everything in a single thread, there's no way a callback can be interrupted. In the above case you will see either 5 or 0, nothing in between.

(Nit - the above code will not compile or run, but I think your point made it across)

> -- 
>   Uri
> 
> Please consider the environment before printing this message.
> http://wwf.panda.org/savepaper/
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python




More information about the Twisted-Python mailing list