[Twisted-Python] Shared resource manioulation example

Nitro nitro at dr-code.org
Fri May 28 14:44:34 EDT 2010


Am 28.05.2010, 20:34 Uhr, schrieb <vitaly at synapticvision.com>:

> hi,
>
> Callbacks serialization is clear to me.
>
> I'll try to rephrase the question: two requests are incoming for  
> reactor()
> to handle. Each request has its own chain of callbacks.
> At some point one of the requests need to update the same
> global data structure, and another request need to delete from the same
> global data structure.
> So theoretically, correct me if I wrong, will be a data collision.
> If it is, than how can I prevent it? Example please.

It will go like this:

reactor loop:

1) receive Event A
    - fire deferred callback
    - deferred callback does data[key] = value
2) receive Event B
     - fire deferred callback
     - del data[key]

So one event is processed after another. Determining if it's a data  
collision is up to you. E.g. you could use something like a revision  
number. Each time you change the dictionary, increase it by one. Then if  
you try to change the dictionary and the client gave a different revision  
id then the last one, you know there is a collision.
What you do sounds very much like database transactions. Those are not  
easy to do. One example is the ZODB which can do what you want. For  
example via BTrees. But I guess there are more lightweight systems which  
do what you want, too. It all depends where you want to go with your app.  
That's up to you though and not really a question in regards to twisted or  
networking.

-Matthias



More information about the Twisted-Python mailing list