简体   繁体   English

Python嵌入式Linux命令

[英]Python inline linux commands

I am testing sorting algorithms and therefore I would like to compine in my Python code, the linux command "time", because it takes some interesting arguments and for example the call of quicksort. 我正在测试排序算法,因此我想在我的Python代码中编写linux命令“ time”,因为它需要一些有趣的参数,例如quicksort的调用。

from subprocess import Popen
import quicksort
import rand

time=Popen("time quicksort.main(rand.main())")

This is tottaly wrong, but it is the closest I managed to get. 这是完全错误的,但这是我设法获得的最接近的结果。 I haven't grasped the idea of subprocess class, is it possible to combine method calls with linux commands, or only add commands in python like "grep..." and send the output to a variable?? 我还没有掌握子进程类的思想,是否可以将方法调用与linux命令结合起来,或者仅在python中添加“ grep ...”之类的命令并将输出发送到变量中?

If you use Popen from subprocess you need to do a lot of things differently. 如果您从子流程中使用Popen,则需要做很多不同的事情。

I believe what you are looking for is check_output, another function belonging to the subprocess module. 我相信您正在寻找的是check_output,这是子流程模块的另一个功能。

But in order to further your understanding, since you are sort-of close, here is what you need to change to get it to work: 但是,为了进一步了解您,由于您已经很接近了,因此需要进行以下更改才能使其正常工作:

The command string "time quicksort.main(rand.main())" is not going to mean anything to bash. 命令字符串"time quicksort.main(rand.main())"对bash毫无意义。 That is python. 那是python。 BUT in the case that it was valid bash language, it would need to be split on word boundaries (like bash WOULD normally do) so you would make it into a list: 但是,如果它是有效的bash语言,则需要在单词边界上进行拆分(就像bash通常会这样做),因此您可以将其分成一个列表:

['time', '...','...']

The only time you can pass Popen a command STRING (not a list) is when you set shell=True in the keywords to Popen. 只有在关键字Popen中设置shell = True时,才可以向Popen传递命令STRING(而不是列表)。

But let's just leave shell at False, do some word-splitting for bash, and pass in a list. 但是,我们只是将shell保留为False,对bash进行一些单词拆分,然后传递一个列表。 On to the next part. 继续下一部分。

Popen returns something you can communicate to/at/with. Popen返回您可以与之通信/与之通信的内容。 Not the result of the process' stdout. 不是该进程的标准输出的结果。 Use subprocess.PIPE for stdin and stdout keywords to Popen. 使用subprocess.PIPE作为Popen的stdin和stdout关键字。

Once you have made a Popen object as described, you can call it's communicate method. 按照说明制作Popen对象后,就可以调用它的communication方法。

The result is two things, stdout and stderr. 结果是两件事,stdout和stderr。

You're after the first one. 你是第一个。 One use case for Popen is for when you need to keep errors and output seperate. Popen的一种用例是当您需要保持错误和分开输出时。 Obviously this isn't turning out to be the best option for inline but oh well. 显然,这并不是内联的最佳选择,但是很好。 Lets deal with stdout. 让我们处理标准输出。

sdtout will probably need to be decoded: sdtout可能需要解码:

stdout.decode()

or perhaps even have newlines stripped as well: 甚至还删除了换行符:

stdout.decode().rstrip()

So as you can see, Popen does not fit the use case you have in mind. 如您所见,Popen不适合您所想到的用例。 There is no need to use subprocess and make system calls in order to time python. 无需使用子进程和进行系统调用即可为python计时。 Look into timeit . 看看timeit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM