擅长:python、mysql、java
<p>与其使用内置的<code>dict</code>,不如考虑使用<code>multiprocessing</code>库中的<code>Manager</code>对象:</p>
<pre><code>from multiprocessing import Manager
from threading import Thread
from time import sleep
manager = Manager()
d = manager.dict()
def do_this(d):
d["this"] = "done"
def do_that(d):
d["that"] ="done"
thread0 = Thread(target=do_this,args=(d,))
thread1 = Thread(target=do_that,args=(d,))
thread0.start()
thread1.start()
thread0.join()
thread1.join()
print d
</code></pre>
<p>这为您提供了一个标准的库线程安全的同步字典,它应该很容易在不改变设计的情况下切换到当前的实现中。在</p>