簡體   English   中英

Python 多處理和共享 memory - 為什么我得到不同的結果?

[英]Python multiprocessing and shared memory - why do I get different results?

我正在測試一些 python 代碼以在 2 個進程之間共享 numpy 數組。

from multiprocessing import Process, Semaphore, shared_memory
import numpy as np
import time

def reader(id, a, shm):
    exst_shm = shared_memory.SharedMemory(name=shm)
    b = np.ndarray(a.shape, dtype=a.dtype, buffer=exst_shm.buf)
        
    time.sleep(2)    
    print('FUNCTION VERSION: ', b[0])   
    

def worker(id, a, shm):
    exst_shm = shared_memory.SharedMemory(name=shm)
    b = np.ndarray(a.shape, dtype=a.dtype, buffer=exst_shm.buf) 
    b[0] += 10 


if __name__ == "__main__":
    a = np.array([0])
    shm = shared_memory.SharedMemory(create=True, size=a.nbytes) 
    c = np.ndarray(a.shape, dtype=a.dtype, buffer=shm.buf)
    
    th1 = Process(target=reader, args=(1, a, shm.name))
    th2 = Process(target=worker, args=(2, a, shm.name))
    
    th1.start()
    th2.start()
    th1.join()
    th2.join()

上面的代碼工作正常,它打印出來: FUNCTION VERSION: 10

from multiprocessing import Process, Semaphore, shared_memory
import numpy as np
import time

class Reader(Process):
    def __init__(self, id, a, shm):
        Process.__init__(self)
        
        exst_shm = shared_memory.SharedMemory(name=shm)
        b = np.ndarray(a.shape, dtype=a.dtype, buffer=exst_shm.buf) 
        
        time.sleep(2)    
        print('SUBCLASS VERSION: ', b[0])   
       
class Worker(Process):
    def __init__(self, id, a, shm):
        Process.__init__(self)
            
        exst_shm = shared_memory.SharedMemory(name=shm) 
        b = np.ndarray(a.shape, dtype=a.dtype, buffer=exst_shm.buf) 
        b[0] += 10
        
if __name__ == "__main__":
    a = np.array([0])
    shm = shared_memory.SharedMemory(create=True, size=a.nbytes) 
    c = np.ndarray(a.shape, dtype=a.dtype, buffer=shm.buf)
    
    th1 = Reader(1, a, shm.name)
    th2 = Worker(2, a, shm.name)

    th1.start()
    th2.start()
    th1.join()
    th2.join()

但是,當它寫成類時,它會打印出: SUBCLASS VERSION: 0。差異來自哪里,代碼有什么問題?

您以錯誤的方式創建課程。

通常start()運行方法run()以在新進程中執行target

當您創建自己的 class 時,您必須覆蓋方法run()並將代碼放在run() ,然后當您使用start()時它將運行此代碼。

在您的版本中,它在主進程中運行您的所有代碼,而不是在新進程中。 首先你創建Reader()睡眠 2s 並打印結果b[0] ,然后你創建Worker()添加b[0] += 10 之后你start()進程 - 但它們無事可做,因為你沒有覆蓋run()並且你在類中沒有target

from multiprocessing import Process, Semaphore, shared_memory
import numpy as np
import time

class Reader(Process):
    def __init__(self, id, a, shm):
        Process.__init__(self)
        
        self.exst_shm = shared_memory.SharedMemory(name=shm)
        self.b = np.ndarray(a.shape, dtype=a.dtype, buffer=self.exst_shm.buf) 
        
    def run(self):            
        time.sleep(2)
        print('SUBCLASS VERSION: ', self.b[0])   
       
class Worker(Process):
    def __init__(self, id, a, shm):
        Process.__init__(self)
        
        self.exst_shm = shared_memory.SharedMemory(name=shm) 
        self.b = np.ndarray(a.shape, dtype=a.dtype, buffer=self.exst_shm.buf) 

    def run(self):            
        self.b[0] += 10
        
if __name__ == "__main__":
    a = np.array([0])
    shm = shared_memory.SharedMemory(create=True, size=a.nbytes) 
    c = np.ndarray(a.shape, dtype=a.dtype, buffer=shm.buf)
    
    th1 = Reader(1, a, shm.name)
    th2 = Worker(2, a, shm.name)

    th1.start()
    th2.start()
    th1.join()
    th2.join()

順便提一句:

使用

import multiprocessing
print(multiprocessing.__file__)

您可以在process.py中找到源代碼並查看它是如何工作的。

def run(self):
    '''
    Method to be run in sub-process; can be overridden in sub-class
    '''
    if self._target:
        self._target(*self._args, **self._kwargs)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM