Python Forum
Updating a list with multiprocessing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Updating a list with multiprocessing
#1
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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:

1
2
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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?
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Updating nested dict list keys tbaror 2 1,939 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Looping through nested elements and updating the original list Alex_James 3 2,908 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  issue with updating list every iteration of a loop ftrillaudp 2 4,070 Oct-29-2018, 03:23 AM
Last Post: ftrillaudp
  List data-member of object not updating inside loop. Sagar 2 4,170 Aug-30-2017, 01:07 PM
Last Post: Sagar
  Updating & Accessing a "Static" List Bass 4 5,421 May-23-2017, 07:12 PM
Last Post: Bass

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020