【问题标题】:Calling pkill to pause a process with subprocess inside a class in python suspends the python script调用 pkill 以在 python 中的类中暂停具有子进程的进程会暂停 python 脚本
【发布时间】:2017-08-20 21:43:20
【问题描述】:

我有一个包含“阻塞方法”的类。在该方法中,我正在听键来执行一些操作。有问题的操作是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...')
                        subprocess.call(['pkill', '-STOP', '-f', 'ffmpeg'])
                    ...

原来ffmpeg进程暂停了,python脚本却被暂停了?我不明白为什么会这样。如果在常规脚本中创建相同的函数(不是在类中更清楚),则不会发生这种情况。我尝试使用 multithreadingmultiprocessing 模块但没有成功。我究竟做错了什么?。谢谢。

【问题讨论】:

    标签: python multithreading ffmpeg subprocess


    【解决方案1】:

    我会自己回答。该问题与subprocess 位于try 块内有关。我解决这个问题的方法是创建一个包含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

    【讨论】:

      猜你喜欢
      • 2017-08-10
      • 2014-12-22
      • 1970-01-01
      • 2019-12-09
      • 2023-01-30
      • 2012-08-12
      • 1970-01-01
      • 2011-02-17
      • 1970-01-01
      相关资源
      最近更新 更多