How to make a multithreaded system work on same dictionary in python
Question: I have a system designed to take data via a socket and store that into a
dictionary to serve as a database. Then all my other modules (GUI, analysis,
write_to_log_file, etc) will access the database and do what they need to do
with the dictionary e.g make widgets/copy the dictionary to a log file. But
since all these things happen at a different rate, I chose to have each module
on their own thread so I can control the frequency.

In the main run function there's something like this:

    
    
        from threading import Thread
        import data_collector
        import write_to_log_file
    
        def main():
            db = {}
            receive_data_thread = Thread(target=data_collector.main, arg=(db,))
            recieve_data_thread.start()  # writes to dictionary  @ 50 Hz
            log_data_thread = Thread(target=write_to_log_file.main, arg(db,))
            log_data_thread.start()  # reads dictionary  @ 1 Hz
    

But it seems that both modules aren't working on the same dictionary instance
because the log_data_thread just prints out the empty dictionary even when the
data_collector shows the data it's inserted into the dictionary.

There's only one writer to the dictionary so I don't have to worry about
threads stepping on each others toes, I just need to figure out a way for all
the modules to read the current database as it's being written.


Answer: [Use a Queue.Queue](http://pymotw.com/2/Queue/#using-queues-with-threads) to
pass values from the reader threads to a single writer thread. Pass the Queue
instance to each `data_collector.main` function. They can all call the Queue's
`put` method.

Meanwhile the `write_to_log_file.main` should also be passed the same Queue
instance, and it can call the Queue's `get` method. As items are pulled out of
the Queue, they can be added to the `dict`.

See also: Alex Martelli, on why [Queue.Queue is the secret sauce of CPython
multithreading](http://stackoverflow.com/a/1192114/190597).

