像个老板一样分工合作
forkme的Python项目详细描述
把你的过程像老板一样分开
importforkmeimportloggingimportosfromtimeimportsleeplogging.basicConfig(level=logging.DEBUG)defmain():print("Master proccess has PID: {0}".format(os.getpid()))forkme.fork(4)print("Proceess #{id} has PID: {pid}".format(id=forkme.get_id(),pid=os.getpid()))sleep(1)if__name__=='__main__':main()
此代码生成4个叉。当您尝试运行它时,您将看到类似这样的内容
Master proccess has PID: 7437 INFO:forkme:Starting 4 processes Proceess #2 has PID: 7440 Proceess #1 has PID: 7439 Proceess #3 has PID: 7441 Proceess #0 has PID: 7438 INFO:forkme:Child with PID: 7439 Number: 1 exited normally INFO:forkme:Child with PID: 7441 Number: 3 exited normally INFO:forkme:Child with PID: 7440 Number: 2 exited normally INFO:forkme:Child with PID: 7438 Number: 0 exited normally
叉子就是控制叉当子进程将被杀死或将退出 非零代码它将立即重新启动。例如:
Master proccess has PID: 7579 INFO:forkme:Starting 4 processes Proceess #0 has PID: 7580 Proceess #1 has PID: 7581 Proceess #2 has PID: 7582 Proceess #3 has PID: 7583 WARNING:forkme:Child with PID: 7580 Number: 0 killed by signal 9, restarting Proceess #0 has PID: 7584 INFO:forkme:Child with PID: 7581 Number: 1 exited normally INFO:forkme:Child with PID: 7582 Number: 2 exited normally INFO:forkme:Child with PID: 7583 Number: 3 exited normally INFO:forkme:Child with PID: 7584 Number: 0 exited normally