-1

I'm just trying to learn multiprocessing with python. I've wrote two basic functions, but apparently they are doesn't work. What could be wrong with these?

startingList = []
evenNumbers = []
unEvenNumbers = []


def fill_list(number):
    for x in range(1, number):
        startingList.append(x)


def test():
    for num in startingList:
        if num % 2 == 0:
            evenNumbers.append(num)
        else:
            unEvenNumbers.append(num)


if __name__ == '__main__':
    start = time.perf_counter()
    p1 = multiprocessing.Process(target=fill_list, args=(17,))
    p2 = multiprocessing.Process(target=test)

    p1.start()
    p2.start()

    p1.join()
    p2.join()
    end = time.perf_counter()

    print(startingList, "\n", evenNumbers, "\n", unEvenNumbers)
  • The process that is running `fill_list()` modifies its own private copy of `startingList`. The process running `test()` has absolutely no access to that list, it only sees its own list which is empty. – jasonharper Nov 09 '21 at 19:09

1 Answers1

1

The issue here is that you are trying to write to startingList in a different process, but that startingList is not the startingList you expect. You need to think a little differently when dealing with multiple processors which have separate memory boundaries.

Check out this answer on how you could resolve this.

vitaliis
  • 4,082
  • 5
  • 18
  • 40
wombat
  • 614
  • 3
  • 18