我寻找过其他问题,并且这个未接受回答的问题是我能找到的唯一一个以某种方式涵盖这个问题并且没有真正帮助的人。另外,我需要它来处理进程,而不是线程。
因此,我从头开始编写了一个示例程序来展示我的问题,您应该能够粘贴它并且它将运行:
import multiprocessing
import time
class Apple:
def __init__(self, color):
self.color = color
def thinkAboutApple(apple):
while True:
print(apple.color)
time.sleep(2)
my_apple = Apple("red")
new_process = multiprocessing.Process(target=thinkAboutApple, args=(my_apple,))
new_process.start()
time.sleep(4)
print("new: brown")
my_apple.color = "brown"
#so that the program doesn't exit after time.sleep(4)
while True:
pass
# actual output | # wanted output
red | red
red | red
new: brown | new: brown
red | brown
red | brown
这告诉我,要么苹果处于一个奇怪的假设,它同时有两种颜色,要么 new_process' 苹果位于 ram 中的另一个位置,并与主进程中的苹果分开。
所以问题是:有没有办法让进程中的苹果指针指向同一个苹果,或者什么是Pythonic方法来保持所有进程中苹果的所有实例相同?如果我在许多进程中有相同的苹果,甚至更多进程没有苹果,我如何确保它们始终相同?
您可以派生一个专门版本Proxy
使用的类multiprocessing.BaseManager
来自(无证)multiprocessing.managers.NamespaceProxy
与基类不同,公开的类all它的方法和属性。这类似于@shtse8 的回答到链接的重复问题,但我在这里发布了一个可运行的答案,以明确它是如何完成的。
from multiprocessing import Process
from multiprocessing.managers import BaseManager, NamespaceProxy
import time
import types
class MyManager(BaseManager): pass # Avoid namespace pollution.
class Apple:
def __init__(self, color):
self.color = color
def Proxy(target):
""" Create a derived NamespaceProxy class for `target`. """
def __getattr__(self, key):
result = self._callmethod('__getattribute__', (key,))
if isinstance(result, types.MethodType):
def wrapper(*args, **kwargs):
self._callmethod(key, args)
return wrapper
return result
dic = {'types': types, '__getattr__': __getattr__}
proxy_name = target.__name__ + "Proxy"
ProxyType = type(proxy_name, (NamespaceProxy,), dic) # Create subclass.
ProxyType._exposed_ = tuple(dir(target))
return ProxyType
AppleProxy = Proxy(Apple)
def thinkAboutApple(apple):
while True:
print(f"apple.color: {apple.color}")
time.sleep(1)
if __name__ == '__main__':
MyManager.register('Apple', Apple, AppleProxy)
manager = MyManager()
manager.start()
my_apple = manager.Apple("red")
new_process = Process(target=thinkAboutApple, args=(my_apple,))
new_process.start()
time.sleep(2) # Allow other process to run a short while.
my_apple.color = "brown" # Change shared class instance.
time.sleep(2) # Allow other process to run at little while longer.
new_process.terminate()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)