[Twisted-Python] How can I keep data integrity in multiple threads environment

Itamar Shtull-Trauring itamar at itamarst.org
Mon Dec 12 12:35:53 EST 2005

On Mon, 2005-12-12 at 16:04 +0100, Kaladan wrote:

> All of the treatments run in threads and thus use shared data. The
> allData
> dictionnary is critical since it can be used by multiple threads. How
> can I keep 
> the allData integrity ? Does Twisted core provide such facilites or do
> I
> have to use semaphore or other stuff ?

"Don't use threads" is a good way to start; unless you're calling into
blocking C libraries using threads won't help you much. Even if you are
(e.g. using Numeric, or DB-API or something), you can still avoid shared
data structures in most cases:

Get data from shared structure in the main (i.e. Twisted's) thread, hand
it to thread that does self-contained operation on data. Thread then
does callFromThread(self.putBackInSharedDataStructure, result), which
will run in the Twisted thread.

More information about the Twisted-Python mailing list