3.3 multiprocess 创建进程
能批量产生进程。
采用Process类创建进程。用于创建代码可控的进程,非脚本执行进程和系统命令进程。
3.3.1 建立两个子进程的Queue通信
创建两个队列,分别作为两个进程的读写管道。
q1 = Queue() q2 = Queue() consoleP = Process(target=script_runner.startConsole, args=(q2, q1,)) httpP = Process(target=httpserver.startServer, args=(q1, q2,)) consoleP.start() time.sleep(3) httpP.start()
script_runner.startConsole :进程入口函数
httpserver.startServer:进程入口函数
如上面代码,可以定义q1队列为:进程consoleP写数据,进程httpP读数据;q2队列为:进程consoleP读数据,进程httpP写数据。
一个进程崩溃,可以根据持有的队列进行恢复。此处存在3个进程,任何一个进程崩溃,均不会影响其他进程。可以设定异常,验证。
例如异常代码:str ="" + 1
缺陷:如果一方启动的进程是httpserver,例如SimpleHttpServer,有自己的进程恢复机制,则无法恢复队列通信。
此时考虑用用pythonConnect类对象处理子进程之间的通信,也适合完全无关的两个进程间的通信,如章节3.3.2。
3.3.2 完全独立进程间的通信
两个完全独立的进程,一般采用命名Pipe或者Socket通信,在windows下,python没有实现命名pipe。另外,python实现了更高层级的通信机制,Listener和Client机制。
Listener服务端:
address = ("localhost", 8000) listener = Listener(address, authkey="password") print "Accept" conn = listener.accept() print