How to use classes with Python multiprocessing?

后端 未结 2 1092

Here\'s some sample code that is reads a file and adds up each line. It is supposed to add up all the numbers from 0-20. However, I always get a result of 0.

<
2条回答
  •  不知归路
    2021-01-27 02:41

    You can accomplish this using shared memory with subprocess.Value, just change your Total class to the following:

    class Total():
        def __init__(self):
            self.total = multiprocessing.Value('d', 0)
    
        def add(self, number):
            self.total.value += int(number)
    
        def __str__(self):
            return str(self.total.value)
    

提交回复
热议问题