相关疑难解决方法(0)

在Python中调用外部命令

如何在Python脚本中调用外部命令(就像我在Unix shell或Windows命令提示符下键入它一样)?

python shell terminal command subprocess

4553
推荐指数
57
解决办法
325万
查看次数

如何在Python中启动后台进程?

我正在尝试将shell脚本移植到更易读的python版本.原始shell脚本在后台使用"&"启动多个进程(实用程序,监视器等).如何在python中实现相同的效果?我希望这些进程不会在python脚本完成时死掉.我确信它与守护进程的概念有某种关系,但我无法轻易找到如何做到这一点.

python daemon process

259
推荐指数
7
解决办法
37万
查看次数

运行进程,不要等待

我想运行一个进程而不是等待它返回.我已尝试使用P_NOWAIT和子进程生成如下:

app = "C:\Windows\Notepad.exe"
file = "C:\Path\To\File.txt"

pid = subprocess.Popen([app, file], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE).pid
Run Code Online (Sandbox Code Playgroud)

但是,控制台窗口保持不变直到我关闭记事本.是否可以启动该过程而不是等待它完成?

python windows

37
推荐指数
3
解决办法
6万
查看次数

Python子进程.check_call vs .check_output

我的python脚本(python 3.4.3)通过子进程调用bash脚本:

import subprocess as sp
res = sp.check_output("bashscript", shell=True)
Run Code Online (Sandbox Code Playgroud)

bashscript包含以下行:

ssh -MNf somehost
Run Code Online (Sandbox Code Playgroud)

这将打开与某个远程主机的共享主连接,以允许一些后续操作.

执行python脚本时,它将提示该ssh行的密码,但在输入密码后它会阻塞,并且永远不会返回.当我按ctrl-C终止脚本时,我看到连接已正确建立(因此ssh行已成功执行).

使用时,我没有这个阻塞问题check_call,而不是check_output,但check_call不检索标准输出.我想了解究竟是什么导致阻塞行为check_output,可能与某些微妙的相关ssh -MNf.

python ssh bash subprocess

17
推荐指数
1
解决办法
2万
查看次数

如何从Python迭代器提供子进程的标准输入?

我正在尝试使用subprocessPython中的模块与读取标准输入并以流式方式写入标准输出的进程进行通信.我希望从生成输入的迭代器获取子进程读取行,然后从子进程读取输出行.输入和输出线之间可能没有一对一的对应关系.如何从返回字符串的任意迭代器中提供子进程?

下面是一些示例代码,它给出了一个简单的测试用例,以及我尝试过的某些方法因某些原因而无法正常工作:

#!/usr/bin/python
from subprocess import *
# A really big iterator
input_iterator = ("hello %s\n" % x for x in xrange(100000000))

# I thought that stdin could be any iterable, but it actually wants a
# filehandle, so this fails with an error.
subproc = Popen("cat", stdin=input_iterator, stdout=PIPE)

# This works, but it first sends *all* the input at once, then returns
# *all* the output as a string, rather than giving me an iterator over
# …
Run Code Online (Sandbox Code Playgroud)

python io subprocess

8
推荐指数
1
解决办法
1853
查看次数

即使使用nohup,子进程也会被杀死

我正在使用subprocess.Popen启动多个进程.

代码是这样的:

while flag > 0:
   flag = check_flag()
   c = MyClass(num_process=10)
   c.launch()
Run Code Online (Sandbox Code Playgroud)

MyClass 如果类似以下内容:

MyClass(object)
   def __init__(self, num_process):
      self.num_process = num_process

   def launch(self):
      if self.check_something() < 10:
         for i in range(self.num_process):
             self.launch_subprocess()

   def launch_subprocess(self):
      subprocess.Popen(["nohup",
                       "python",
                       "/home/mypythonfile.py"],
                       stdout=open('/dev/null', 'w'),
                       stderr=open('logfile.log', 'w'),
                       shell=False)
Run Code Online (Sandbox Code Playgroud)

在大多数情况下,启动的子流程有时会在运行过程中死亡.在某些情况下,它完成.

但是,如果我subprocess.Popen在while循环中直接使用,则该过程会继续并及时完成.

有人能告诉我如何通过上面描述的方式使用子进程来让进程在后台运行?

python subprocess nohup

6
推荐指数
1
解决办法
2885
查看次数

标签 统计

python ×6

subprocess ×4

bash ×1

command ×1

daemon ×1

io ×1

nohup ×1

process ×1

shell ×1

ssh ×1

terminal ×1

windows ×1