前段时间因需求撸了一把多进程。在本地windows环境测试的效果挺好的。上线后,跟我交互的php男哥炸了。男哥说他每个请求两次,他删不完了。那天我,男哥和我老大查bug到晚上12点,才确定是多进程的原因。下面我们来看看python多进程。
python多进程 没有必需__name__=“main”。但windows 平台有特殊要求
Since Windows has no fork, the multiprocessing module starts a new Python process and imports the calling module. If Process() gets called upon import, then this sets off an infinite succession of new processes (or until your machine runs out of resources). This is the reason for hiding calls to Process() inside
if __name__ == "__main__"
since statements inside this if-statement will not get called upon import.
也就是说在windows多进程必须加if name == “main”,因为windows没有fork
原代码只判断进程存活,然后定时查看进程进程,进程退出重启。但是在linux上由于我把flask启动也加上了,导致flask重复加载,然后同一个进程就启动两次,所以php那边收到两次请求,php也没有做并发处理,所以,线上app数据重复了。
从fork()函数原型来看,它也属于一个内建函数。子进程永远返回0,而父进程返回子进程的ID。这样做的理由是,一个父进程可以fork()出很多子进程,所以,父进程要记下每个子进程的ID,而子进程只需要调用getppid()就可以拿到父进程的ID。
由于在docker内部运行,所以第一次的ppid等于0。以后测试切记环境的差异,哈哈哈