启动一个完全独立的进程 [英] Launch a completely independent process

查看:48
本文介绍了启动一个完全独立的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的 python 脚本 main.py 启动一个进程.具体来说,我想运行以下命令:

`nohup python ./myfile.py &`

并且文件 myfile.py 应该继续运行,即使在 main.py 脚本退出后也是如此.

我也想得到新进程的pid.

我试过了:

  • os.spawnl*
  • os.exec*
  • subprocess.Popen

main.py 脚本退出时,所有这些都将终止 myfile.py.

<小时>

更新:我可以将 os.startfilexdg-open 一起使用吗?这是正确的方法吗?

<小时>

示例

a = subprocess.Popen([sys.executable, "nohup/usr/bin/python25/long_process.py &"],stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)打印一个.pid

如果我检查 ps aux |grep long_process,没有进程在运行.

long_process.py 继续打印一些文本:没有退出.

我在这里做错了什么吗?

解决方案

你打开你长期运行的进程并保持一个管道.所以你希望与它交谈.当您的启动器脚本退出时,您将无法再与它交谈.长时间运行的进程收到一个SIGPIPE并退出.

以下对我有用(Linux、Python 2.7).

创建一个长时间运行的可执行文件:

$ echo "sleep 100" >~/tmp/sleeper.sh

运行 Python REPL:

$ python>>>导入子流程导入操作系统p = subprocess.Popen(['/bin/sh', os.path.expanduser('~/tmp/sleeper.sh')])# 看,没有管道!打印 p.pid# 打印 29893

退出 REPL 并查看进程仍在运行:

<预><代码>>>>^D$ ps ax |grep 卧铺29893 pts/0 S 0:00/bin/sh .../tmp/sleeper.sh29917 pts/0 S+ 0:00 grep --color=auto sleeper

如果您想先与启动的进程通信,然后让它继续运行,您有几个选择:

  • 在你长期运行的进程中处理 SIGPIPE,不要死在它上面.启动器进程退出后,无需标准输入即可存活.
  • 使用参数、环境或临时文件传递您想要的任何内容.
  • 如果您想要双向通信,请考虑使用命名管道(man mkfifo)或一个套接字,或者编写一个合适的服务器.
  • 在初始双向通信阶段完成后,使长时间运行的进程分叉.

I want to initiate a process from my python script main.py. Specifically, I want to run the below command:

`nohup python ./myfile.py &`

and the file myfile.py should continue running, even after the main.py script exits.

I also wish to get the pid of the new process.

I tried:

  • os.spawnl*
  • os.exec*
  • subprocess.Popen

and all are terminating the myfile.py when the main.py script exits.


Update: Can I use os.startfile with xdg-open? Is it the right approach?


Example

a = subprocess.Popen([sys.executable, "nohup /usr/bin/python25 /long_process.py &"],
     stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
print a.pid

If I check ps aux | grep long_process, there is no process running.

long_process.py which keeps on printing some text: no exit.

Am I doing anything wrong here?

解决方案

You open your long-running process and keep a pipe to it. So you expect to talk to it. When yor launcher script exits, you can no longer talk to it. The long-running process receives a SIGPIPE and exits.

The following just worked for me (Linux, Python 2.7).

Create a long-running executable:

$ echo "sleep 100" > ~/tmp/sleeper.sh

Run Python REPL:

$ python
>>>

import subprocess
import os
p = subprocess.Popen(['/bin/sh', os.path.expanduser('~/tmp/sleeper.sh')])
# look ma, no pipes!
print p.pid
# prints 29893

Exit the REPL and see the process still running:

>>> ^D
$ ps ax | grep sleeper
29893 pts/0    S      0:00 /bin/sh .../tmp/sleeper.sh
29917 pts/0    S+     0:00 grep --color=auto sleeper

If you want to first communicate to the started process and then leave it alone to run further, you have a few options:

  • Handle SIGPIPE in your long-running process, do not die on it. Live without stdin after the launcher process exits.
  • Pass whatever you wanted using arguments, environment, or a temporary file.
  • If you want bidirectional communication, consider using a named pipe (man mkfifo) or a socket, or writing a proper server.
  • Make the long-running process fork after the initial bi-direcional communication phase is done.

这篇关于启动一个完全独立的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆