python command timeout

python에서 command 실행시 타임아웃 설정

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import subprocess

'''
command 실행시 타임아웃을 걸고 stdout은 PIPE로 출력하여
'RESULT'가 검색된다면 'RESULT' 아래 라인을 반환
'''

def executeCmdTimeout( strCmd, timeout=60 ):
        # subprocess.Popen은 명령어는 실행시 fork하여 실행됨.
        p = subprocess.Popen( strCmd, stdout=subprocess.PIPE, shell=True )
        startTime = time.time()

        while p.poll() is None:
                time.sleep(0.1) # 100ms 마다 폴링하여 시간 확인

                nowTime = time.time()
                if nowTime - startTime > timeout: # 타임아웃이 되었다면
                        p.kill()
                        return ''

        lines = p.stdout.read().split('\n') # line단위로 스플릿

        # stdout에 RESULT라는 스트링이 존재한다면 바로 아래 라인을 반환
        idx = 0
        while idx < len(lines):
                if lines[idx][:len('RESULT')] == 'RESULT':
                        return lines[idx+1:]

                idx += 1

        return None


>>>> advanced 코드글 링크

댓글 1개:

  1. I made the advanced code.

    you can refer to my code

    https://holyzo.blogspot.com/2019/07/python-command-advanced.html

    답글삭제