更新多处理中的对象.Manager.dict()



我想知道如何更新在不同进程之间分配为共享字典值的对象。我有以下课程:


class Task:
STATUS_PROCESSING = 0
STATUS_EXECUTING = 1
STATUS_QUEUED = 2
STATUS_TERMINATED = 3
STATUS_HALTED = 4
STATUS_STOPPED = 5
def __init__(self, id: str, uuid: str, options: dict):
self.id = id
self.uuid = uuid
self.options = options
self.state = 0
# Some properties...
def execute(self):
""" Executes the task
"""
# Set self status to Executing
self.state = Task.STATUS_EXECUTING
print('Executing...')
self.state = Task.STATUS_TERMINATED

它只是创建一个具有给定ID的新任务,并在调用execute()时执行其核心方法。我有另一个带有静态方法的类,它用于向dict添加一个新的对(id,task(,并读取dict执行其所有任务,直到主程序停止:

class DummyList:
@staticmethod
def submit_task(d: dict, uuid: str, options: dict):
""" Submit a new task
"""
# If invalid UUID
if not Task.is_valid_uuid(uuid):
return False
# If more than 20 tasks
if len(d) > 19:
return False
# Create random ID (simplified for question)
r_id = str(random.randint(1, 2000000))
if r_id in d:
return False
# Add task to the dictionary
d[r_id] = Task(r_id, uuid, options)
# Set status to queue
d[r_id].state = Task.STATUS_QUEUED
# Return the created ID
return r_id
@staticmethod
def execute_forever(d):
try:
while True:
for i in d.values():
print(i.state)
i.execute()
time.sleep(5)
except KeyboardInterrupt:
pass

问题是DummyList.execute_forever()将从另一个进程调用,而主进程将执行submit_task(...)函数来添加新任务。像这样:

# Create a shared dict
m = multiprocessing.Manager()
shared_d = m.dict()
# Start the Task shared list execution in another process
p = multiprocessing.Process(target=DummyList.execute_forever, args=(shared_d,))
# Set the process to exit when the main halts
p.daemon = True
p.start()
........

# From another place
# The message variable is not important
DummyList.submit_task(shared_d, message['proc'], message['options'])

它有效!任务被创建、分配到字典并执行,但以下几行(在上面的代码中可以看到(没有正确执行:

self.state = Task.STATUS_EXECUTING
self.state = Task.STATUS_TERMINATED
d[r_id].state = Task.STATUS_QUEUED

如果我们尝试在整个代码中写入ìf shared_d[<some_id>].state == 0,它将始终是True,因为属性不会更新

我想这是因为当对象属性被修改时,共享字典不会更新,也许是因为字典只知道他必须在调用getitem

setitem非常感谢!

我终于找到了解决方案。除非调用了代理字典中的__getitem____setitem__方法,否则字典中的对象不会更新。这就是为什么我更改了以下行:

任务

execute()方法以return self结束。self.state必须在整个执行过程中进行更改。

任务管理器

方法更改为:

@staticmethod
def execute_forever(d):
""" Infinite loop reading the queued tasks and executing all of them.
"""
try:
while True:
# Notice the loop using the keys
for i in d.keys():
# Execute and re-assign item
d[i] = d[i].execute()
time.sleep(5)
except KeyboardInterrupt:
pass

相关内容

  • 没有找到相关文章

最新更新