现在我们是一些正在编写日间交易机器人的人,但它不是重点。在函数上调用Process的常用方法是嵌入式的,并且在结构良好的已经很大的类中非常模糊。
问题
让我们假设一个进程可能永远不会终止并将输出写入我们在def main()
中访问的队列。这个实现是pythonic还是跟随pep20 ..一个很好的样板?:
from multiprocessing import Process
from multiprocessing import Queue
from time import sleep
class Worker(Process):
def __init__(self, q, *args, **kwargs):
super().__init__(*args, **kwargs)
self.q = q
def run(self):
# doing work put work in queue
self.q.put("hello foo")
class RestartingProcesses(Process):
def __init__(self, ps=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ps_classes = {} if ps is None else ps
self.processes = []
def setup(self):
"""
Initiate all processes that is in the dict all inserts into the list
"""
for p, args in self.ps_classes.items():
p = p(args)
p.start()
self.processes += [p]
def run(self):
"""
Uses the process.__class__ to instantiate and get appropriate args
of process in the dict
"""
self.setup()
# should be while True:
for i in range(8):
for p in self.processes:
if not p.is_alive():
print("worker dead")
p.join()
self.processes.remove(p)
p = p.__class__(self.ps_classes[p.__class__])
p.start()
self.processes += [p]
sleep(1)
def main():
q = Queue()
processes_to_start_with_args = {Worker: q}
r = RestartingProcesses(processes_to_start_with_args)
r.start()
r.join()
r.terminate()
while not q.empty():
print(q.get())
if __name__ == "__main__":
main()
您有什么经历并以其他方式解决了这个问题?