Alberto Munoz
Alberto Munoz

Reputation: 1

Is there a way to create a shared list using multiprocessing in python and append to that list?

I am new to python and don't have much experience. But I need to use the multiprocessing feature and I've run into some errors. I've looked across the threads and haven't found a solution to this issue. But, I am trying to create a shared list that I can store data into. I don't want to use the a predesignated array size like the multiprocessing.array because it can very widely. So I would like something like a .append that you can use in a list. I've tried using a list manager like I've seen here but it gives me the error that is "ValueError:I/O operation on closed file" Here is an example of what I've tried to do

import multiprocessing

def dosomething(dlist,i):
    dlist.append(i)

if __name__ == "__main__"
    processes = []
    manager = multiprocessing.Manager()
    mlist = manager.list()
    
    for i in range(0,1):
          p = multiprocessing.Process(target = dosomething,args = ([mlist] , i))
          p.start()
          processes.append(p)

    for process in processes:
          process.join()

This code isn't anything specific to what i want to do. I just tried to get something similar to what I want to do to work

Upvotes: 0

Views: 888

Answers (1)

Nechoj
Nechoj

Reputation: 1582

Here is a working example (on my computer):

import multiprocessing

def dosomething(dlist,i):
    dlist.append(i)

if __name__ == "__main__":
    processes = []
    manager = multiprocessing.Manager()
    mlist = manager.list()
    
    for i in range(4):
          p = multiprocessing.Process(target = dosomething,args = (mlist , i))
          p.start()
          processes.append(p)

    for process in processes:
          process.join()
         
    print(mlist)

Changes to your code:

  1. add colon (:) after if __name__ == "__main__"
  2. pass mlist without brackets
  3. extend range to range(4)

Upvotes: 1

Related Questions