Python Forum
Updating a list with multiprocessing - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Updating a list with multiprocessing (/thread-27680.html)



Updating a list with multiprocessing - bRitch022 - Jun-16-2020

I am writing a program to control a machine, and this machine needs to have a central registry to keep track of all parameters about the machine (Inputs/outputs, configuration values, etc.). I've set up a list of zeros using the numpy library, and I am able to write to this list correctly. The problem, however, is when I attempt to then read that same list from another process or directly from my main code. I have verified that the data is updating to the list by performing a read action directly after writing. But reading from it from another function shows that it has not been changed.

Here is my code:
#import numpy as np

class Registry(object):
    def __init__(self, registry_size=128, register_size=16):
        self.registry_size = registry_size
        self.register_size = register_size
        
        self.registry = np.zeros((registry_size, register_size), dtype=int)
        self.buff = np.zeros((1,16)) # Buffer for data in/out

    def write16_register(self, reg_addr, data):

       # Convert data to 16 digit binary and sign extend
       data = int(data)
            
       # Split data into 8 bit MSB and LSB
       data_MSB = (data & 0xFF00) >> 8
       data_LSB = (data & 0x00FF)
            
       # Load MSBs to buff
       self.buff = format(data_MSB, '08b')
       
       # Write MSBs to registry
       for i in range(0,8,1):
           self.registry[reg_addr][i] = self.buff[i]

       # Load LSB to buffer
       self.buff = format(data_LSB, '08b')

       # Write LSB to registry
       for i in range(8,16,1):
           self.registry[reg_addr][i] = self.buff[i-8]
                
       print(self.registry[reg_addr])
if __name__ == '__main__':

  p1 = multithreading.Process(target=
The final print(self.registry[reg_addr]) successfully prints the written values. However the following does NOT work:

 def read_register(self, reg_addr):
    print(self.registry[reg_addr])

Standby for edited post... For some reason it did not save my edits. The above post is not up to date

import numpy as np
import multiprocessing

class Registry(object):
    def __init__(self, registry_size=128, register_size=16):
        self.registry_size = registry_size
        self.register_size = register_size
        
        self.registry = np.zeros((registry_size, register_size), dtype=int)
        self.buff = np.zeros((1,16)) # Buffer for data in/out
    
    def write16_register(self, reg_addr, data):
            # Convert data to 16 digit binary and sign extend
            data = int(data)
            
            # Split data into 8 bit MSB and LSB
            data_MSB = (data & 0xFF00) >> 8
            data_LSB = (data & 0x00FF)
            
            # Load MSB
            self.buff = format(data_MSB, '08b')

            for i in range(0,8,1):
                self.registry[reg_addr][i] = self.buff[i]

            # Load LSB
            self.buff = format(data_LSB, '08b')

            for i in range(8,16,1):
                self.registry[reg_addr][i] = self.buff[i-8]
                
            print(self.registry[reg_addr])

    def read_register(self, reg_addr):
      print(self.registry[reg_addr])

if __name__ == '__main__':
    Main_Registry = Registry()
    p1 = multiprocessing.Process(target=Main_Registry.write16_register, args=(0x00, 0xB594,))
    p2 = multiprocessing.Process(target=Main_Registry.read_register, args=(0x00,))
        
    p1.start()
    p2.start()
    
    p1.join()
    p2.join()
Quote:Output:
[1 0 1 1 0 1 0 1 1 0 0 1 0 1 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

As you can see, the print statement inside the write16_register shows that the registry has been written to. But the print statement in read_register shows that it is unchanged and still zeroes.

Does anyone know why I might be running into this issue?


RE: Updating a list with multiprocessing - bRitch022 - Jul-15-2020

This question was not very well formulated, especially with the code edits, and I don't blame anyone for not attempting a reply. I rewrote the question, and found a solution here