在两个线程进程之间访问数据 [英] Access data between two threading processes
问题描述
我们正在尝试访问两个线程之间的数据,但无法完成此操作.我们正在寻找一种简单(优雅)的方式.
We are trying to access data between two threads, but are unable to accomplish this. We are looking for an easy (and elegant) way.
这是我们当前的代码.
目标:完成第二个线程/进程后,实例B
中的listHolder
必须包含2个项目.
This is our current code.
Goal: after the second thread/process is done, the listHolder
in instance B
must contain 2 items.
Class A:
self.name = "MyNameIsBlah"
Class B:
# Contains a list of A Objects. Is now empty.
self.listHolder = []
def add(self, obj):
self.listHolder.append(obj)
def remove(self, obj):
self.listHolder.remove(obj)
def process(list):
# Create our second instance of A in process/thread
secondItem = A()
# Add our new instance to the list, so that we can access it out of our process/thread.
list.append(secondItem)
# Create new instance of B which is the manager. Our listHolder is empty here.
manager = B()
# Create new instance of A which is our first item
firstItem = A()
# Add our first item to the manager. Our listHolder now contains one item now.
b.add(firstItem)
# Start a new seperate process.
p = Process(target=process, args=manager.listHolder)
# Now start the thread
p.start()
# We now want to access our second item here from the listHolder, which was initiated in the seperate process/thread.
print len(manager.listHolder) << 1
print manager.listHolder[1] << ERROR
- 预期输出:
listHolder
中的2个A
实例. - 获得的输出:
listHolder
中的1个A
实例. - Expected output: 2
A
instances inlistHolder
. - Got output: 1
A
instance inlistHolder
.
我们如何使用分离的进程/线程访问管理器中的对象,以便它们可以以非线程阻塞的方式同时运行两个函数.
How can we access our objects in the manager with the use of a seperated process/threads, so they can run two functions simultaneously in a non-thread-blocking way.
当前,我们正在尝试通过进程来实现这一目标,但是如果线程能够以一种更简单的方式实现这一目标,那么这将不是问题.使用了Python 2.7.
Currently we are trying to accomplish this with processes, but if threads can accomplish this goal in a easier way, then its not a problem. Python 2.7 is used.
更新1:
@James Mills回答是使用".join()".但是,这将阻塞主线程,直到完成第二个进程.我尝试使用此方法,但是本示例中使用的流程将永远不会停止执行(为True时).它将用作计时器,必须必须能够迭代到列表并从列表中删除对象.
@James Mills replied with using ".join()". However, this will block the main thread until the second Process is done. I tried using this, but the Process which is used in this example will never stop execution (while True). It will act as a timer, which must be able to iterate to a list and remove objects from the list.
有人建议如何完成此工作并解决当前的cPickle错误吗?
Anyone has any suggestion how to accomplish this and fix the current cPickle error?
推荐答案
进程之间的共享状态是使用multiprocessing.Manager
类在进程之间同步数据(内部使用Queue
的):
One of the simplest ways of Sharing state between processes is to use the multiprocessing.Manager
class to synchronize data between processes (which interally uses a Queue
):
示例:
from multiprocessing import Process, Manager
def f(d, l):
d[1] = '1'
d['2'] = 2
d[0.25] = None
l.reverse()
if __name__ == '__main__':
manager = Manager()
d = manager.dict()
l = manager.list(range(10))
p = Process(target=f, args=(d, l))
p.start()
p.join()
print d
print l
输出:
bash-4.3$ python -i foo.py
{0.25: None, 1: '1', '2': 2}
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>>
注意:请谨慎处理您正在共享并附加到您的Process
类的对象类型,因为这样最终可能会出现酸洗问题.请参阅: Python多处理酸洗错误
Note: Please be careful with the types of obejcts ou are sharing and attaching to your Process
classes as you may end up with issues with pickling. See: Python multiprocessing pickling error
这篇关于在两个线程进程之间访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!