从 python 中执行命令行程序 [英] Executing command line programs from within python

查看:31
本文介绍了从 python 中执行命令行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 Web 应用程序,它将处理(填充、混合、合并等)声音文件,我发现 sox 正是我想要的.Sox 是一个 linux 命令行程序,我对让 python Web 应用程序根据每个请求在我的服务器上启动新的 sox 进程感到有点不舒服.

I'm building a web application that will is going to manipulate (pad, mix, merge etc) sound files and I've found that sox does exactly what I want. Sox is a linux command line program and I'm feeling a little uncomfortable with having the python web app starting new sox processes on my server on a per request basis.

示例:

import os
os.system('sox input.wav -b 24 output.aiff rate -v -L -b 90 48k')

这整个设置对我来说似乎有点不稳定.

This whole setup seems a little unstable to me.

所以我的问题是,从 Python(或任何脚本语言)Web 应用程序中运行命令行程序的最佳实践是什么?

So my question is, what's the best practice for running command line programs from within a python (or any scripting language) web app?

消息队列是实现整个请求响应周期的一件事.但是还有其他方法可以让这些东西更优雅吗?

Message queues would be one thing to implement in order to get around the whole request response cycle. But is there other ways to make these things more elegant?

推荐答案

subprocess 模块是从 Python 运行其他程序的首选方式——比 os.system 更灵活、更好用.

import subprocess
#subprocess.check_output(['ls', '-l'])  # All that is technically needed...
print(subprocess.check_output(['ls', '-l']))

这篇关于从 python 中执行命令行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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