Hello everyone, 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()