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
user4015351
2 Answers 2
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
J'e
3,8965 gold badges39 silver badges69 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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!
Comments
lang-py
Queue? Check out this example: docs.python.org/2/library/…shared_array_base = multiprocessing.Array(ctypes.c_double, 10) shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())