0

How i can share an array as in the code below with an array and not a single value (in this example there is a counter as you can see)?

How i can append and remove elements from the array?

class mp_counter(object):
 def __init__(self, initval=0):
 self.val = multiprocessing.Value('i', initval)
 self.lock = multiprocessing.Lock()
 def increment(self):
 with self.lock:
 self.val.value += 1
 def decrement(self):
 with self.lock:
 self.val.value -= 1
 def value(self):
 with self.lock:
 return self.val.value
counter = mp_counter(0)
proc = threading.Thread(target=start_processes,kwargs={"counter":counter})
proc.daemon = True
proc.start()

Thank you in advance

asked Apr 27, 2017 at 15:23
2
  • 1
    What about the Queue? Check out this example: docs.python.org/2/library/… Commented Apr 27, 2017 at 15:42
  • What about shared arrays: shared_array_base = multiprocessing.Array(ctypes.c_double, 10) shared_array = np.ctypeslib.as_array(shared_array_base.get_obj()) Commented Apr 27, 2017 at 15:44

2 Answers 2

1

check out from multiprocessing import Array. Array has a "lock" argument that has a default of True for thread safe reason. another option would be from multiprocessing import RawArray which does not have "lock" built in.

from multiprocessing import Array, Process
import numpy as np
def worker(shared_arr, proc_number):
 shared_arr[proc_number] = 1
shared_arr = Array('f', 4)
print("initialArray:", np.array(shared_arr))
# will print [ 0. 0. 0. 0.]
num_procs = 2
processes = []
for proc_number in range(2):
 p = threading.Thread(target=worker, args=(shared_arr, proc_number,))
 p.daemon = True
 processes.append(p)
[p.start() for p in processes]
[p.join() for p in processes]
print("results:", np.array(shared_arr))
# will print [ 1. 1. 0. 0. ]
answered Apr 27, 2017 at 16:26
Sign up to request clarification or add additional context in comments.

Comments

0

Using your code I'm +1 the whole array. Is that what you want?

import multiprocessing
import ctypes
import threading
import numpy as np
class mp_counter(object):
 def __init__(self, initval=0):
 shared_array_base = multiprocessing.Array(ctypes.c_double, initval)
 self.val = np.ctypeslib.as_array(shared_array_base.get_obj())
 self.lock = multiprocessing.Lock()
 def increment(self):
 with self.lock:
 self.val += 1
 def decrement(self):
 with self.lock:
 self.val -= 1
 def value(self):
 with self.lock:
 return self.val
def start_processes(counter):
 print("Before:", counter)
 m = mp_counter(counter)
 m.increment()
 print("After: ", m.value())
if __name__ == '__main__':
 counter = [1., 2., 3., 4.]
 proc = threading.Thread(target=start_processes,kwargs={"counter":counter})
 proc.daemon = True
 proc.start()
 proc.join()

The output is:

Before: [1.0, 2.0, 3.0, 4.0]
After: [ 2. 3. 4. 5.]

I hope it helped!

answered Apr 27, 2017 at 16:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.