使用嵌套类在进程之间共享队列 (Python)

发布于 2024-09-29 15:35:55 字数 1847 浏览 3 评论 0原文

我有一个关于 Python 进程之间共享队列的问题。下面,我有三个队列、一个主进程和三个内部进程。每个内部进程都会从各个队列中添加和获取值(它们需要轻松访问队列)。

我认为它现在就可以工作,但是这段代码是我将要从事的一个大项目的基础,我想确保没有更好的方法来做到这一点,我不知道。我只是想出了这个主意。从其他一些帖子看来,嵌套类不太像 Python。

有什么建议吗?这段代码容易读还是很难读?放弃嵌套类还是保持原样?

谢谢大家。

class MainController(Process):
    def __init__(self):
        self.queue_stream   = Queue()
        self.queue_language = Queue()
        self.queue_expander = Queue()

        self.control_stream   = self.StreamController(self).start()
        self.control_language = self.LanguageController(self).start()
        self.control_expander = self.ExpanderController(self).start()

        print 'Launching Main Controller'

    class StreamController(Process):
        def __init__(self, main):
            Process.__init__(self)
            self.main = main
            print 'Launching Stream Controller'

        def run(self):
            while True:
                self.main.queue_stream.put('hello, stream')

    class LanguageController(Process):
        def __init__(self, main):
            Process.__init__(self)
            self.main = main
            print 'Launching Language Controller'

        def run(self):
            while True:
                print self.main.queue_stream.get()
                self.main.queue_language.put('hello, language')

    class ExpanderController(Process):
        def __init__(self, main):
            Process.__init__(self)
            self.main = main
            print 'Launching Expander Controller'

        def run(self):
            while True:
                print self.main.queue_language.get()
                self.main.queue_expander.put('hello, expander')

def main():
    # Launch all queues for the system
    control_main = MainController()

if __name__ == '__main__':
    print 'Launching System...'
    main()

I have a question about sharing queues between processes in Python. Below, I have three queues, one main process, and three inner processes. Each inner process will be adding and getting values from the various queues (they need easy access to the queues).

I think it works as it is right now, but this code is the foundation of a big project I'm going to work on and I want to make sure that there is not a better way of doing this that I don't know about. I just sort of came up with this idea. It appears from some other posts that nested classes is not very Python-like.

Any advice? Is this code easy or hard to read? Abandon nested classes or leave it as it is?

Thanks all.

class MainController(Process):
    def __init__(self):
        self.queue_stream   = Queue()
        self.queue_language = Queue()
        self.queue_expander = Queue()

        self.control_stream   = self.StreamController(self).start()
        self.control_language = self.LanguageController(self).start()
        self.control_expander = self.ExpanderController(self).start()

        print 'Launching Main Controller'

    class StreamController(Process):
        def __init__(self, main):
            Process.__init__(self)
            self.main = main
            print 'Launching Stream Controller'

        def run(self):
            while True:
                self.main.queue_stream.put('hello, stream')

    class LanguageController(Process):
        def __init__(self, main):
            Process.__init__(self)
            self.main = main
            print 'Launching Language Controller'

        def run(self):
            while True:
                print self.main.queue_stream.get()
                self.main.queue_language.put('hello, language')

    class ExpanderController(Process):
        def __init__(self, main):
            Process.__init__(self)
            self.main = main
            print 'Launching Expander Controller'

        def run(self):
            while True:
                print self.main.queue_language.get()
                self.main.queue_expander.put('hello, expander')

def main():
    # Launch all queues for the system
    control_main = MainController()

if __name__ == '__main__':
    print 'Launching System...'
    main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弄潮 2024-10-06 15:35:55

我建议您使用线程模块而不是进程。我建议您仅当子类扩展父类的功能时才使用嵌套类。

class   WorkerThread(threading.Thread):

另一个建议是在子线程之间使用共享锁,以防止共享队列上的竞争条件。

            tasks_lock.acquire()
            ret = tasks_queue.get()
            tasks_lock.release()

看一下这个示例

I recommend you to use the threading module instead of process. I suggest you to use nested classes only if the child classes are extending on functionality the parent class.

class   WorkerThread(threading.Thread):

Another recommendation is to use a shared lock between your child threads in order to prevent race conditions on your shared Queue.

            tasks_lock.acquire()
            ret = tasks_queue.get()
            tasks_lock.release()

Take a look on this Example

难忘№最初的完美 2024-10-06 15:35:55

孩子现在必须知道父亲的实施情况。我不鼓励这样做

def infinite_producer(queue):
    while True:
        queue.put('hello, stream')

class MainController(Process):
    def __init__(self):
        self.queue_stream   = Queue()
        self.queue_language = Queue()
        self.queue_expander = Queue()
        self.self.control_stream = Process(target=infinite_producer,self.queue_stream)

    def run(self):
        self.control_stream.start()

 #... etc you get the idea.

if __name__ == '__main__':
    print 'Launching System...'
    control_main = MainController()
    control_main.start()

The child now have to know about the implementation of the father. I discourage that

def infinite_producer(queue):
    while True:
        queue.put('hello, stream')

class MainController(Process):
    def __init__(self):
        self.queue_stream   = Queue()
        self.queue_language = Queue()
        self.queue_expander = Queue()
        self.self.control_stream = Process(target=infinite_producer,self.queue_stream)

    def run(self):
        self.control_stream.start()

 #... etc you get the idea.

if __name__ == '__main__':
    print 'Launching System...'
    control_main = MainController()
    control_main.start()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文