使用嵌套类在进程之间共享队列 (Python)
我有一个关于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您使用线程模块而不是进程。我建议您仅当子类扩展父类的功能时才使用嵌套类。
另一个建议是在子线程之间使用共享锁,以防止共享队列上的竞争条件。
看一下这个示例
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.
Another recommendation is to use a shared lock between your child threads in order to prevent race conditions on your shared Queue.
Take a look on this Example
孩子现在必须知道父亲的实施情况。我不鼓励这样做
The child now have to know about the implementation of the father. I discourage that