分叉并分离当前进程。
detach的Python项目详细描述
分叉并分离当前进程
[![生成状态](https://travis-ci.org/BlueDragonX/detach.svg)](https://travis-ci.org/BlueDragonX/detach)
用法
detach包包含一个名为detach的上下文管理器。它被使用 with withwith语句派生当前进程并在其中执行代码 过程。当上下文管理器退出时子进程退出。以下 参数可以传递到分离以更改其行为:
- stdout-将子stdout重定向到此流。
- stderr-将子stderr重定向到此流。
- stdin-将其流重定向到子stdin。
- 关闭fds-关闭子级中除stdio之外的所有文件描述符
- exclude\u fds-如果close\u fds为True,则不要关闭这些文件描述符
示例
###带标准输出的简单叉
import detach, os, sys
- with detach.Detach(sys.stdout) as d:
- if d.pid:
- print(“forked child with pid {}”.format(d.pid))
- else:
- print(“hello from child process {}”.format(os.getpid()))
###守护
import detach from your_app import main
- def main():
- “”“Your daemon code here.”“”
- with detach.Detach(daemonize=True) as d:
- if d.pid:
- print(“started process {} in background”.format(pid))
- else:
- main()
###调用外部命令
import detach, sys pid = detach.call([‘bash’, ‘-c’, ‘echo “my pid is $$”’], stdout=sys.stdout) print(“running external command {}”.format(pid))
许可证
版权所有(c)2014瑞安资产阶级。这个项目及其所有内容是 根据包含在[许可证][1]中的BSD衍生许可证进行许可 文件。
[1]:https://github.com/bluedragonx/detach/blob/master/LICENSE“许可证”