如何从烧瓶启动和停止子过程

时间:2019-08-08 21:51:23

标签: python flask subprocess

我有一个flask应用程序,从中我需要启动其他一些应用程序(webrtc聊天)。 我可以用gunicorn轻松地做到这一点,但是我的问题是如何启动5-10-15或更多的webrtc应用程序(可以使用gunicorn命令执行此操作),然后停止其中的一些但不是全部。

因此:

type ParentProps = { a: number }
type ParentState = { b: number }
class MyComponent<P, S> extends React.Component<ParentProps & P, ParentState & S> {}
let parentComponent = <MyComponent a={1} />

type ChildProps = { c: number }
type ChildState = { d: number }
class ExtendedComponent extends MyComponent<ChildProps, ChildState> {}
let childComponent = <ExtendedComponent a={1} c={2} />

命令启动服务器,每个管理器都会在自己的端口上启动服务器,这里没有问题。但是,当管理器不再需要服务器时,如何存储进程然后将其杀死?我不知道-有什么提示吗?

gunicorn --worker-class eventlet -w 1 --certfile /path/to/file --keyfile /path/to/file -b 0.0.0.0:8000 wsgi:app

所以我可以使用上面的命令启动服务器,但是不知道如何存储然后终止进程, 感谢您的时间和帮助!

1 个答案:

答案 0 :(得分:0)

如果有人需要,How to terminate a python subprocess launched with shell=True的想法似乎可行

p = subprocess.Popen("exec " + cmd, stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)
# and after, we can kill this group with
os.killpg(os.getpgid(p.pid), signal.SIGTERM)