python 调用shell

来源:互联网 发布:防止蹭网软件 编辑:程序博客网 时间:2024/06/07 22:03

python 在调用系统命令时,如linux  shell,可以用os或者subprocess模块,下面介绍subprocess模块

实例:

from subprocess import Popen, PIPEimport datetimeurl_temporary = "/home/adger/Videos/542eea47e4b0e0471d7cff61_v1.orig.mov"video_width = "640"hideo_height = "360"frame_rate = 25video_new_path = "/home/adger/Videos/test1.mp4"result = Popen(    "ffmpeg -i '%s' -s %sX%s -vcodec libx264 -profile:v main -preset slow -acodec libfaac -b:a 64k -r %s '%s'" % (        url_temporary, video_width, hideo_height, frame_rate,        video_new_path),    stdout=PIPE, stderr=PIPE, stdin=PIPE, shell=True,close_fds=True)timeout = 600datetime_start = datetime.datetime.now()while True:    datetime_end = datetime.datetime.now()    res = result.poll()    if res == 0:        print result.pid, "end"        break    elif res is None:        if (datetime_end - datetime_start).seconds > timeout:            print "timeout",result.pid            result.terminate()            result.kill()            break

result.pid:父进程ID

result.terminate(),杀掉子进程

result.kill(),杀掉父进程


1 0