2013-11-15 29 views
0

我正在使用python模塊Para​​miko ssh到Linux框並執行兩個C程序。 程序1在觸發器上的DAQ設備上生成一個信號。它等待觸發並在5秒內終止。 程序2生成觸發器。分叉使用python paramiko

這裏是我的Python類:

class test: 
    server = "localhost" 
    username = "root" 
    password = "12345" 
    def ssh (self, cmd = ""): 
      ssh = paramiko.SSHClient() 
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
      ssh.connect(self.server,username=self.username,password=self.password) 
      stdin, stdout, stderr = ssh.exec_command(cmd) 
      result = stdout.read().splitlines() 
      if len(result)!= 0: 
       return result 
      else: 
       return -1 

test.ssh("program1") 
test.ssh("program2") 

的問題是程序1已被終止後,正在執行的程序2,因此不會產生任何東西。 有沒有一種方法可以比program1更快地運行program2?我試過

test.ssh("program1 &") 
test.ssh("program2") 

但無濟於事。如果我在兩個終端shell中手動運行這些程序,那麼它工作正常。有什麼想法嗎?

+0

執行這兩個程序,你可以嘗試使用Python線程 – c4f4t0r

回答

1

可以使用線程或者多在不同的會話

import multiprocessing 

input = ["program1","program2"] 

for i in range(2): 
    p = mutiprocessing.Process(target=test.ssh,args=(input[i],)) 
    p.start() 
    processlist.append(p) 

for p in processlist: 
    p.join() 
0

您可以嘗試在同一SSH會話中運行它們:test.ssh("program1 & program2")。這樣你就不會爲所做的所有設置和拆卸付出兩倍的費用,你會做test.ssh

+0

我不需要這種行爲。我想隨時運行test.ssh(「program2」),而不是在test.ssh(「program1」)之後 – maslick