2014-09-22 96 views
1

我想創建一個腳本,可以同時運行兩個基於Linux的工具,並且基於將它們的輸出寫入單個結果文件?多線程Linux工具命令?

我對所有的誠實都做了一些研究,如os.fork等等,真的只是尋找一些指導。

我目前使用subprocess.call([command here])運行一個命令並將其輸出到文件中,但我只是想知道如何同時運行兩個工具,比如。

subprocess.call([command 1 >> results.txt]) 
subprocess.call([command 2 >> results.txt]) 

兩者都發生在同一時間。

回答

1

如果您想同時以call塊的形式運行,則首先您需要撥打Popen而不是call,直到過程結束。您也可以使用stdout參數將輸出傳遞到像對象這樣的文件。

with open("results.txt", "w") as results: 
    p1 = subprocess.Popen(["command1"], stdout=results) 
    p2 = subprocess.Popen(["command2"], stdout=results) 
    p1.wait() 
    p2.wait()