回调中的子进程和共享自对象

时间:2017-08-26 14:01:13

标签: python callback subprocess producer-consumer

我有一个典型的多个生产者,一个消费者使用python子进程和一个队列。 消费者具有对另一个对象的回调。尽管该对象与所有子进程共享,但是当子进程完成时,对象的更改将丢失。

以下是代码:

from multiprocessing import Process, Queue
import random
import time

class Manager(object):
    def __init__(self):
        self.queue = Queue()

    def consume(self, call_back):
        while True:
            task = self.queue.get()
            if task is None:
                self.queue.close()
                break
            time.sleep(0.05)
            call_back(task)
            print("Queue got task: {}.".format(task))

    def produce(self, value):
        time.sleep(random.uniform(0.1, 1.0))
        task = "TSK {}".format(value)
        self.queue.put(task)


    def start(self, call_back, n_tasks=10):

        consumer = Process(target=self.consume, args=(call_back,))
        consumer.start()

        workers = [Process(target=self.produce,args=(i,))
            for i in range(n_tasks)]

        for w in workers:
            w.start()
        for w in workers:
            w.join()

        self.queue.put(None)
        consumer.join()


class Display(object):
    def __init__(self):
        self.tasks = []

    def display_tasks(self, n_tasks=10):
        def add_task(task):
            self.tasks.append(task)
            print("Dislaying tasks so far: {}".format(self.tasks))
        Manager().start(add_task, n_tasks)
        print("Total tasks: {}".format(self.tasks))


Display().display_tasks(5)

输出结果为:

Dislaying tasks so far: ['TSK 3']
Queue got task: TSK 3.
Dislaying tasks so far: ['TSK 3', 'TSK 4']
Queue got task: TSK 4.
Dislaying tasks so far: ['TSK 3', 'TSK 4', 'TSK 2']
Queue got task: TSK 2.
Dislaying tasks so far: ['TSK 3', 'TSK 4', 'TSK 2', 'TSK 0']
Queue got task: TSK 0.
Dislaying tasks so far: ['TSK 3', 'TSK 4', 'TSK 2', 'TSK 0', 'TSK 1']
Queue got task: TSK 1.
Total tasks: []

我在期待:

Total tasks: ['TSK 3', 'TSK 4', 'TSK 2', 'TSK 0', 'TSK 1']

任何想法如何在没有:

的情况下完成
class Display(object):
    def __init__(self):
        manager = Manager()
        self.tasks = manager.list()

事实上,在实际案例中,这是使用QTableWidget,表中的每个条目都是QTableWidgetItem ....

这是真正的回叫功能(data_set_tableQTableWidget):

def _add_item(data):
        row = dc.size()
        self._content.data_set_table.insertRow(row)
        for i in range(len(data)):
            if data[i] is not None:
                item = QtGui.QTableWidgetItem(str(data[i]))
                item.setFlags( QtCore.Qt.ItemIsSelectable |  QtCore.Qt.ItemIsEnabled )
                self._content.data_set_table.setItem(row, i, item)

1 个答案:

答案 0 :(得分:0)

答案似乎是使用Manager。我将删除它中的答案qith PyQt

来自多处理导入Process,Queue,Manager 来自multiprocessing.managers导入BaseManager
随机导入 进口时间

class Scheduler(object):
    def __init__(self):
        self.queue = Queue()

    def consume(self, call_back):
        while True:
            task = self.queue.get()
            if task is None:
                self.queue.close()
                break
            time.sleep(0.05)
            print("Queue got task: {}.".format(task))
            call_back(task)

    def produce(self, value):
        time.sleep(random.uniform(0.1, 1.0))
        task = "TSK {}".format(value)
        self.queue.put(task)


    def start(self, call_back, n_tasks=10):

        consumer = Process(target=self.consume, args=(call_back,))
        consumer.start()

        workers = [Process(target=self.produce,args=(i,))
            for i in range(n_tasks)]

        for w in workers:
            w.start()
        for w in workers:
            w.join()

        self.queue.put(None)
        consumer.join()

class CustomTable(object):
    def __init__(self):
        self.contents = []

    def addItem(self, item):
        self.contents.append(item)

    def display(self):
        print(self.__repr__)

    def __repr__(self):
        res = 80*"-" + "\n" + str(self.contents) + "\n" + 80*"-" + "\n"
        return res

class CustomTableManager(BaseManager):  
    pass  

CustomTableManager.register('CustomTable', CustomTable, exposed = ['contents', 'addItem', 'display', '__repr__'])  

customTableManager = CustomTableManager()  
customTableManager.start()  

class Display(object):
    def __init__(self):
        # self.tasks = CustomTable()
        self.tasks = customTableManager.CustomTable()

    def display_tasks(self, n_tasks=10):
        def add_task(task):
            self.tasks.addItem(task)
            print("Dislaying tasks so far:\n{}".format(self.tasks))
        Scheduler().start(add_task, n_tasks)
        print("Total tasks:\n{}".format(self.tasks))

Display().display_tasks(5)