Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

python Multi processing share object modification not expected

I have got a consumer class like the following:

class Consumer(multiprocessing.Process):
    def __init__(self, best_list, task_queue)
        multiprocessing.Process.__init__(self)
        self.best_list = best_list
    self.task_queue = task_queue
    def get_best_list(self):
        return self.best_list

    def run(self):
        proc_name = self.name
        while True:
            next_task = self.task_queue.get()
            answer = next_task()
            if answer != '' and answer != []:
                self.best_list += answer()
                print (self.best_list)

class Task:
    def __call__(self):
        return [100]

best_list = []


tasks = multiprocessing.JoinableQueue()
for i in range(100):
    tasks.put(Task())

c = Consumer(best_list, tasks)
c.start()

tasks.join()

print (c.get_best_list())
print (best_list)

The result would shows c.get_best_list() and best_list are both as [], but the print (self.best_list) shows the self.best_list is appending.

I have double checked with my code but still not working out the solution and not sure why get_best_list is returning the initial state.

Would someone please give some hints

question from:https://stackoverflow.com/questions/65642886/python-multi-processing-share-object-modification-not-expected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...