<p>不幸的是,<code>multiprocessing.Manager()</code>不支持<code>deque</code>,但它可以与<code>list</code>、<code>dict</code>、<code>Queue</code>、<code>Value</code>和{<cd7>}。一个<code>list</code>非常接近,所以我在下面的示例中使用了它。。在</p>
<pre><code>from multiprocessing import Process, Manager, Lock
import numpy as np
class TestClass:
def __init__(self):
self.maxlen = 4
self.manager = Manager()
self.mem = self.manager.list()
self.lock = self.manager.Lock()
self.process = Process(target=self.run, args=(self.mem, self.lock))
def run(self, mem, lock):
while True:
array = np.random.randint(0, high=10, size=5)
with lock:
if len(mem) >= self.maxlen:
mem.pop(0)
mem.append(array)
def print_values(mem, lock):
while True:
with lock:
print mem
test = TestClass()
print_process = Process(target=print_values, args=(test.mem, test.lock))
test.process.start()
print_process.start()
test.process.join()
print_process.join()
</code></pre>
<p>在使用管理器对象时要小心一点。你可以像它们引用的对象一样使用它们,但是你不能做像。。。<code>mem = mem[-4:]</code>以截断值,因为您正在更改被引用对象。在</p>
<p>至于编码风格,我可能会将<code>Manager</code>对象移到类之外,或者将<code>print_values</code>函数移到类内,但举个例子,这是可行的。如果要移动对象,只需注意不能在<code>run</code>方法中直接使用<code>self.mem</code>。您需要在启动进程时传递它,否则python在后台执行的<code>fork</code>将创建一个新实例,并且不会共享它。在</p>
<p>希望这对你的情况有用,如果不行,我们可以尝试调整一下。在</p>