北雨南萍

鹏徙南冥三千里,抟扶直上九重天

python进程池编程实例

本实例基于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()
阅读更多
个人分类: 语言: python
上一篇failed to load ldlinux.c32
下一篇Ubuntu/Debian上安装Node.js和npm
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭