Mailing List Archive

shared info: safety and performance questions
I have some questions regarding safety and performance of shared
variables. The situation: I'm writing a multithreaded, event-driving
GUI application. Some events create new threads, but all GUI calls
are handled in the main thread, so no worries there. First, safety:

I don't want to deal with semaphores/mutexes/etc. unless necessary.
What python commands are considered atomic and threadsafe? I would
especially be interested in append() and del being safe, as well as
mutation of lists and dictionaries through reassignment.

Next, performance. Functions in different threads and idlefuncs need
to share data. One func might need list a, another might need dict b,
another may need tuple c, and another might need a and c but not b.
Would it be faster to send each mutable variable where it belongs, or
stuff them all in a giant dictionary which gets passed to each shared
function regardless of its need, which looks up only the appropriate
keys?

Thanks for the help!
shared info: safety and performance questions [ In reply to ]
[nathan@islanddata.com]
> ...
> I don't want to deal with semaphores/mutexes/etc. unless necessary.
> What python commands are considered atomic and threadsafe? I would
> especially be interested in append() and del being safe, as well as
> mutation of lists and dictionaries through reassignment.

See the new FAQ entry:

4.88. What kinds of global value mutation are thread-safe?

at (for the next week <wink>):

http://grail.cnri.reston.va.us/cgi-bin/faqw.py?req=recent&days=7

> Next, performance. Functions in different threads and idlefuncs need
> to share data. One func might need list a, another might need dict b,
> another may need tuple c, and another might need a and c but not b.
> Would it be faster to send each mutable variable where it belongs, or
> stuff them all in a giant dictionary which gets passed to each shared
> function regardless of its need, which looks up only the appropriate
> keys?

Those are difficult design decisions that have less to do with performance
than keeping things working under modification. No easy answers. Note that
(within reason) giant dicts are no slower than tiny dicts, so if you want to
organize your shared data that way there's not a performance argument
against it.

the-best-way-to-share-data-is-not-to-ly y'rs - tim