1

Let's say that I have an empty list my_list and 5 processes pr1, pr2, pr3, pr4, pr5, where each of them appends something to my list.

My question is : Is something like this possible? And will it behave normaly or an error will occur?

SnuKies
  • 1,578
  • 1
  • 16
  • 37

2 Answers2

1

If these processes are independent then something like this is not possible. At least not without some additional mechanisms like sockets (which greatly increases complexity).

If these are created via multiprocessing and my_list is just a list then each process will have its own copy of the list.

Again in multiprocessing if you define my_list as multiprocessing.Queue then indeed this will be a shared structure between processes.

freakish
  • 54,167
  • 9
  • 132
  • 169
  • But if my processes will return each a list, then I could concatenate all of them in the list I need, right? – SnuKies Aug 31 '16 at 08:18
  • @SnuKies Normally a process doesn't return anything. It just ends its execution. However Python does give a neat abstraction that allows that. Are you talking about `multiprocessing.map`? I'm not sure, try it. – freakish Aug 31 '16 at 08:22
  • I'm really new to `multiprocessing`. Currently I am working to a project where I'm installing multiple components using `multiprocessing` and I thought I could use `multiprocessing` to determine which of those components have changed since last time I installed them and which have not. – SnuKies Aug 31 '16 at 08:26
1

Are all 5 of your processes applying the same function, but to different inputs? If this is the case, then using Pool.map() from multiprocessing is appropriate:

from multiprocessing import Pool

n_processes = 5 #number of processes to use
input = [0.,1.,2.,3.] #some input

def f(x):
    #the function being applied on each thread
    return x**2. #for example

p = Pool(n_processes)
my_list = p.map(f,input)
p.close() #close the pool now you're finished with it

So the function f(x) is being applied to different inputs on each process, and then the output is collected in a list (equivalent to your 'my_list' that was empty to begin with). The output array is ordered in the same way as the input array i.e. if input = [x1,x2,x3,x4,x5] then output=[f(x1),f(x2),f(x3),f(x4),f(x5)].

Angus Williams
  • 2,284
  • 19
  • 21