我有一个python脚本,它接受一个输入,将其格式化为一个命令,调用服务器上的另一个脚本,然后使用子进程执行:import sys, subprocessthingy = sys.argv[1]command = usr/local/bin/otherscript.pl {0} .format(thin...

我有一个python脚本,它接受一个输入,将其格式化为一个命令,调用服务器上的另一个脚本,然后使用子进程执行:
import sys, subprocess
thingy = sys.argv[1]
command = 'usr/local/bin/otherscript.pl {0} &'.format(thingy)
command_list = command.split()
subprocess.call(command_list)
我附加&到最后因为otherscript.pl需要一些时间来执行,我更喜欢在后台运行.但是,脚本似乎仍然执行而没有让我重新控制shell,我必须等到执行完成后才能回到我的提示符.有没有其他方法可以使用子进程在后台完全运行脚本?
解决方法:
&安培;是一个shell功能.如果您希望它与子进程一起使用,则必须指定shell = True,如:
subprocess.call(command, shell=True)
这将允许您在后台运行命令.
笔记:
>由于shell = True,以上使用命令,而不是command_list.
>使用shell = True启用所有shell的功能.除非包含thingy的命令来自您信任的来源,否则不要这样做.
更安全的选择
此替代方法仍允许您在后台运行该命令但是安全,因为它使用默认的shell = False:
p = subprocess.Popen(command_list)
执行此语句后,该命令将在后台运行.如果要确保它已完成,请运行p.wait().
本文标题为:python – 在后台执行子进程


基础教程推荐
- python正则表达式之re.match()与re.search()的用法及区 2022-08-30
- Pytorch使用transforms 2023-08-04
- 使用CType时从Windows DLL检测调用Python脚本 2023-11-13
- 基于Python实现评论区抽奖功能详解 2023-08-09
- Python中的getattr,__getattr__,__getattribute__和__get__详解 2023-08-09
- python-Windows的* .npy阅读器 2023-11-13
- Python 如何手动编写一个自己的LRU缓存装饰器的方法实现 2023-08-04
- python 列表,集合和字典的增删改查 2023-08-11
- python多进程并发 2023-09-03
- python-如何在Google App Engine灵活环境中编辑NGINX配置? 2023-11-13