基本信息
源码名称:python 并发执行cmd命令(Linux可用)
源码大小:1.34KB
文件格式:.py
开发语言:Python
更新时间:2020-08-24
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
并发执行cmd命令

import datetime
import os
import threading
 
def execCmd(cmd):
    try:
        print "命令%s开始运行%s" % (cmd,datetime.datetime.now())
        os.system(cmd)
        print "命令%s结束运行%s" % (cmd,datetime.datetime.now())
    except Exception, e:
        print '%s\t 运行失败,失败原因\r\n%s' % (cmd,e)
 
if __name__ == '__main__':
    # 需要执行的命令列表
    cmds = ['ffmpeg -re -i /home/vmuser/work/sy/output.mp4 -vcodec libx264 -acodec aac -pkt_size 1316 -f mpegts udp://127.0.0.1:1234',
           'ffmpeg -i udp://127.0.0.1:1234 -vcodec copy -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://127.0.0.1:1935/live','ffmpeg -i "rtmp://127.0.0.1:1935/live" -c copy -f hls -hls_time 8 -hls_list_size 3 -hls_wrap 3 /usr/local/nginx/html/hls/output.m3u8']
    
    #线程池
    threads = []
    
    print "程序开始运行%s" % datetime.datetime.now()
 
    for cmd in cmds:
        th = threading.Thread(target=execCmd, args=(cmd,))
        th.start()
        threads.append(th)
         
    # 等待线程运行完毕
    for th in threads:
        th.join()
         
    print "程序结束运行%s" % datetime.datetime.now()