다음 대기열에서 데이터를 처리하는 유휴 백그라운드 프로세스가 있습니다.이 프로세스는 다음과 같이 구현했습니다. 이 예제에서 전달 된 데이터는 단지 정수이지만 최대 1000 개의 정수를 가진 목록을 전달하고 초당 큐에 최대 100 개의 목록을 넣을 것입니다. 이것이 올바른 접근 방법입니까, 아니면 더 정교한 RPC 및 서버 메소드를 찾아야합니까?유휴 프로세스와의 파이썬 프로세스 간 통신
import multiprocessing
import Queue
import time
class MyProcess(multiprocessing.Process):
def __init__(self, queue, cmds):
multiprocessing.Process.__init__(self)
self.q = queue
self.cmds = cmds
def run(self):
exit_flag = False
while True:
try:
obj = self.q.get(False)
print obj
except Queue.Empty:
if exit_flag:
break
else:
pass
if not exit_flag and self.cmds.poll():
cmd = self.cmds.recv()
if cmd == -1:
exit_flag = True
time.sleep(.01)
if __name__ == '__main__':
queue = multiprocessing.Queue()
proc2main, main2proc = multiprocessing.Pipe(duplex=False)
p = MyProcess(queue, proc2main)
p.start()
for i in range(5):
queue.put(i)
main2proc.send(-1)
proc2main.close()
main2proc.close()
# Wait for the worker to finish
queue.close()
queue.join_thread()
p.join()