我正在尝试创建一个fork,以同时执行Python脚本和Flask Restful API。问题是当我创建fork并执行打印以查看子进程是否被成功创建/执行时,打印执行两次。
代码如下:
if __name__ == "__main__":
if sys.argv[1] == "run":
snortListenerPID = os.fork()
if snortListenerPID == 0:
print("I'm the child process")
else:
app.run(debug=True,host="0.0.0.0", port=80)
elif sys.argv[1] == "config":
#controllerInterpreter.interpreterMainLoop()
interpreterMainLoop()
else:
print("ERROR - Invalid command line option...")
当我运行上面的代码时,我得到以下输出:
I'm the child process
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
* Restarting with stat
I'm the child process
* Debugger is active!
* Debugger PIN: 186-348-493
从输出中的可以看到在终端上两次打印了“我是孩子的进程”。我用这个叉子做错了吗?有什么办法解决这个问题?