简体   繁体   English

在python中的多个进程之间共享类变量

[英]Share a class variable across multiple processes in python

I have a class variable declared as a list that I want to update from a method declared within that class. 我有一个类变量声明为列表,我想从该类中声明的方法进行更新。 However since this method processes a large amount of data, I am using multiprocessing to invoke it and hence I need to put lock on the class variable before updating it. 但是,由于此方法处理大量数据,因此我使用多重处理来调用它,因此在更新它之前需要锁定类变量。 I am unable to figure out how to put such a lock and update the class variable. 我无法弄清楚如何放置这样的锁并更新类变量。 If it matters, I am only creating one object of the said class at any given time. 如果很重要,我在任何给定时间都只创建一个上述类的对象。

Because of python's GIL, multiprocessing can only be used whith completely separate tasks, and no shared memory. 由于python的GIL,只能在完全独立的任务且没有共享内存的情况下使用多处理。 But you still can make it happend by using multiprocessing shared Array/Value: 但是您仍然可以通过使用多处理共享数组/值来实现它:

from https://docs.python.org/2/library/multiprocessing.html#sharing-state-between-processes 来自https://docs.python.org/2/library/multiprocessing.html#sharing-state-between-processes

from multiprocessing import Process, Value, Array

def f(n, a):
    n.value = 3.1415927
    for i in range(len(a)):
        a[i] = -a[i]

if __name__ == '__main__':
    num = Value('d', 0.0)
    arr = Array('i', range(10))

    p = Process(target=f, args=(num, arr))
    p.start()
    p.join()

    print num.value
    print arr[:]

Now as you asked, you need to ensure that differents processes won't access the same variable at the same time, and use a Lock . 现在,按照您的要求,您需要确保differents进程不会同时访问同一变量,并使用Lock Hopefuly, all the shared variable available in the multiprocessing module are paired with a Lock. 希望所有在multiprocessing模块中可用的共享变量都与一个锁配对。

To access the lock : 要访问锁:

num.acquire() # get the lock
# do stuff
num.release() # don't forget to release it

I hope this helps. 我希望这有帮助。

If you're using the multiprocessing module (as opposed to multi threading , which is different), then unless I'm mistaken, the multiple processes forked don't share memory and each process would have its own copy of your class. 如果您使用的是多处理模块(与之不同的是多线程 ),那么除非我没有记错,否则分叉的多个进程不会共享内存,并且每个进程都有自己的类副本。 This would mean that a lock would not be necessary, but it would also mean that the class attribute is not shared like you want it to be. 这将意味着将不需要锁,但也将意味着不会像您希望的那样共享class属性。

The multiprocessing module does offer several ways to allow communication between processes, including shared array objects. 多处理模块确实提供了几种方法来允许进程之间进行通信,包括共享数组对象。 Perhaps this is what you're looking for. 也许这就是您要寻找的。

Depending on what you're doing, you might also consider using the master-worker pattern, where you create a worker class with methods to manipulate your data, spawn several processes to run this class, and then dispatch datasets to the workers from your main process using the Queue class from the multiprocessing module. 根据您的工作,您可能还考虑使用master-worker模式,在该模式中创建一个带有方法的工作程序类,以操纵数据的方法,产生几个运行该类的过程,然后从主程序中将数据集分发给工作程序使用来自多处理模块的Queue类进行处理。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM