2017-03-28 84 views
0

我有一個包含「阻塞方法」的類。在該方法中,我正在聽按鍵來執行一些操作。有問題的操作是pkill。下面我將其粘貼:調用pkill以暫停Python中的類中的子進程的進程暫停python腳本

def show_control(self, control): 
    """docstring for control"""                               
    if control == True:   
     from mkchromecast.getch import getch, pause 

     self.controls_msg()  
     try:     
      while(True):  
       key = getch() 
       if(key == 'p'):  
        if self.videoarg == True: 
         print('Pausing Casting Process...') 
         subprocess.call(['pkill', '-STOP', '-f', 'ffmpeg']) 
        ... 

事實證明,該ffmpeg過程被暫停,但python腳本被暫停?我不明白爲什麼會這樣。如果在普通腳本中創建相同的功能(不是在課堂內部更清晰),這不會發生。我試過使用multithreadingmultiprocessing模塊,但沒有成功。我究竟做錯了什麼?。謝謝。

回答

0

我會回答自己。該問題與位於try區塊內的subprocess有關。我做了什麼來解決這個問題是創建一個包含subprocess啓動pkill命令的新方法:

def show_control(self, control): 
"""docstring for control"""                               
if control == True:   
    from mkchromecast.getch import getch, pause 

    self.controls_msg()  
    try:     
     while(True):  
      key = getch() 
      if(key == 'p'):  
       if self.videoarg == True: 
        print('Pausing Casting Process...') 
        action = 'pause' 
        self.ffmpeg_handler(action) 
       ... 


def ffmpeg_handler(self, action): 
    if action == 'pause': 
     subprocess.call(['pkill', '-STOP', '-f', 'ffmpeg']) 

here