本实例基于Python的进程池类multiprocessing.Pool,
并对每个进程的结果进行检测;
#!/usr/bin/env python
#coding=utf-8
"""
File: test-pool.py
Author: Hank
Last modified: 2015/6/15 11:40
Desc:
"""
from multiprocessing import Process, Pool
import re
import string
import subprocess
from time import sleep
import types
def detect(rtmp):
rtmpdump = ['./rtmpdump-detect', '--live']
rtmpdump_set = ['-r', rtmp, '--timeout', '5', '--stop', '3', '-o','/dev/null']
rtmpdump_cmd = rtmpdump + rtmpdump_set
str_rtmpdump_cmd =(' ').join(rtmpdump_cmd)
print "RTMPDUMP_CDM =%s" %(str_rtmpdump_cmd)
rtmpdump_proc = subprocess.Popen(rtmpdump_cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(stdoutdata, stderrdata) = rtmpdump_proc.communicate()
print "Process output"
print ("STDOUT: " + stdoutdata)
code = '100'
if rtmpdump_proc.returncode != 0:
print (str_rtmpdump_cmd + " ERROR!")
pattern = re.compile(r'ret=(\d)')
for r in str(stdoutdata).split("\n"):
try:
code = pattern.search(r).group(1)
print "CODE : %s"%(code)
except Exception:
code = code
return "done: " + code
def main():
pool = Pool(processes=2)
result = []
rtmp_list = [ 'rtmp://192.168.1.9/live/abcdefg',
'rtmp://192.168.1.9/live/abcdefgvv',
'rtmp://192.168.1.9/live/abcdefgvv',
'rtmp://192.168.1.9/live/abcdefgtv',
'rtmp://192.168.1.99/live/abcdefgv',
'rtmp://192.168.1.99/live/abcdefgv']
for rtmp in rtmp_list:
print "RTMP: %s" %(rtmp)
result.append(pool.apply_async(detect, (rtmp,)))
pool.close()
pool.join()
for res in result:
print res.get()
if __name__ == "__main__":
main()
阅读(2124) | 评论(0) | 转发(0) |