简体   繁体   English

从在多处理器进程中运行的模块内部更新对象属性

[英]Updating object attributes from within module run in multiprocessor process

I am relatively new to python and definitely new to multiprocessing. 我对python相对较新,对于多处理绝对是新的。 I'm following this question/answer for the structure of my multiprocessing, but in def func_A , I'm calling a module that passes a class object as one of the arguments. 我正在按照这个问题/答案进行多处理的结构,但是在def func_A ,我正在调用一个将类对象作为参数之一传递的模块。 In the module, I change an object attribute that I would like the main program to see and update the user with the object attribute value. 在该模块中,我更改了一个对象属性,我希望主程序可以看到该对象属性并使用该对象属性值更新用户。 The child processes run for very long times, so I need the main program to provide updates as they run. 子进程运行时间很长,因此我需要主程序在运行时提供更新。

My suspicion is that I'm not understanding namespace/object scoping or something similar, but from what I've read, passing an object (an instance of a class?) to a module as an argument passes a reference to the object and not a copy. 我的怀疑是我不了解名称空间/对象作用域或类似的东西,而是从我所读的内容中,将对象(类的实例?)作为参数传递给模块,而不是对对象的引用。复印件。 I would have thought this meant that changing the attributes of the object in the child process/module would have changed the attributes in the main program object (since they're the same object). 我以为这意味着更改子进程/模块中对象的属性会更改主程序对象中的属性(因为它们是同一对象)。 Or am I confusing things? 还是让我感到困惑?

The code for my main program: 我的主程序的代码:

# MainProgram.py
import multiprocessing as mp
import time
from time import sleep
import sys
from datetime import datetime
import myModule

MYOBJECTNAMES = ['name1','name2']

class myClass:
  def __init__(self, name):
    self.name = name
    self.value = 0

myObjects = []
for n in MYOBJECTNAMES:
  myObjects.append(myClass(n))

def func_A(process_number, queue):
  start = datetime.now()
  print("Process {} (object: {}) started at {}".format(process_number, myObjects[process_number].name, start))
  myModule.Eval(myObjects[process_number])
  sys.stdout.flush()

def multiproc_master():
  queue = mp.Queue()
  proceed = mp.Event()

  processes = [mp.Process(target=func_A, args=(x, queue)) for x in range(len(myObjects))]
  for p in processes:
    p.start() 

  for i in range(100):
    for o in myObjects:
      print("In main: Value of {} is {}".format(o.name, o.value))
    sleep(10)

  for p in processes:  
    p.join()

if __name__ == '__main__':
  split_jobs = multiproc_master()
  print(split_jobs)

The code for my module program: 我的模块程序的代码:

# myModule.py
from time import sleep

def Eval(myObject):

  for i in range(100):
    myObject.value += 1
    print("In module: Value of {} is {}".format(myObject.name, myObject.value))
    sleep(5)

That question/answer you linked to probably was probably a poor choice to use as a template, as it's doing many things that your code doesn't require (much less use). 您链接到的问题/答案可能不太适合用作模板,因为它可以执行代码不需要的很多事情(使用更少)。

I think your biggest misconception about how multiprocessing works is thinking that all the code is running in the same address-space. 我认为您对多处理工作原理的最大误解是认为所有代码都在同一地址空间中运行。 The main task runs in its own, and there are separate ones for each subtask. 主要任务独立运行,每个子任务都有单独的任务。 The way your code is written, each of them will end up with its own separate myObjects list. 编写代码的方式, 每个代码都会以其自己的独立myObjects列表结尾。 That's why the main task doesn't see any of the changes made by any of the other tasks. 这就是为什么主任务看不到其他任务进行的任何更改的原因。

While there are ways share objects using the multiprocessing module, doing so often introduces significant overhead because keeping it or them all in-sync between all the processes requires lots of things happening "under the covers" to make seem like they're shared (which is what is really going on since they can't actually be because of having separate address-spaces). 虽然使用方法共享对象multiprocessing模块,这样做往往引入显著的开销,因为保持它还是他们都在同步的所有进程之间需要大量的东西“在幕后”,使发生似乎像他们共享(其中真正发生了什么,因为它们实际上不是因为有单独的地址空间而导致的)。 This overhead frequently completely cancels out any speed gained by parallel-processing. 这种开销经常会完全抵消并行处理获得的任何速度。

As stated in the documentation: " when doing concurrent programming it is usually best to avoid using shared state as far as possible ". 如文档中所述:“ 在进行并发编程时,通常最好尽可能避免使用共享状态 ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用多处理器模块中的管理器更新嵌套字典 - Update a nested dict with a Manager from multiprocessor module 为什么进程 object 的属性在运行后不保留? - Why are attributes of a Process object not kept after run? 从模块 main 中运行单个测试 - Run a single test from within module main 在 Dynamo 中更新 object 的某些属性 - Updating certain attributes of an object in Dynamo 获取可从模块或任何其他对象访问的所有属性 - Getting all the attributes reachable from a module or any other object 如何从另一个模块的对象中更改模块变量的值? - How to change the value of a module variable from within an object of another module? 当在Electron应用程序中作为子进程运行时,Python将找不到已安装的模块 - Python won't find installed module when run as child process within Electron app 如何修复多处理器 TypeError: 'list' object is not callable - How to fix a multiprocessor TypeError: 'list' object is not callable 如何在python脚本中运行&#39;module load &lt;&gt;&#39;命令 - How to run 'module load < > ' command from within python script 在单独的线程中运行 Websocket,更新 class 属性 - Run Websocket in a separate Thread, updating class attributes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM