在Python中并行执行两个进程

时间:2013-01-11 01:53:58

标签: python shell parallel-processing

我尝试使用以下代码并行执行两个命令10秒,但整个过程需要10秒以上,如输出中所示。你能不能帮助我更好地理解这个问题的原因和最佳解决方案。

stime = datetime.datetime.now()
print stime
commands = ("sudo /usr/local/bin/snort -v -u snort -g snort -c /usr/local/snort/etc/snort.conf -i eth0 &", "sudo gedit test")
for p in commands:
    p = subprocess.Popen(shlex.split(p), stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
    class Alarm(Exception):
        pass
    def alarm_handler(signum, frame):
        raise Alarm
    signal.signal(signal.SIGALRM, alarm_handler)
    signal.alarm(10) #in seconds
    try:
        stdoutdata, stderrdata = p.communicate()
        signal.alarm(0) #reset the alarm
    except Alarm:
        print 'Ooops, taking too long!!!!'
etime = datetime.datetime.now() 
print etime

输出:

2013-01-08 03:30:00.836412
Ooops, taking too long!!!!
2013-01-08 03:30:16.548519

2 个答案:

答案 0 :(得分:2)

我觉得threading.Timer可能更合适:

from threading import Timer
from subprocess import Popen,PIPE
import shlex
import datetime
import sys

jobs = ['sleep 100','sleep 200']

timers = []
processes = []
print datetime.datetime.now()
for job in jobs:
    p = Popen(shlex.split(job),stdout = PIPE)
    t = Timer(10,lambda p=p: p.terminate())
    t.start()
    timers.append(t)
    processes.append(p)

for t in timers:
    t.join()

stdout,stderr = processes[0].communicate()    
stdout,stderr = processes[1].communicate()
print datetime.datetime.now()

答案 1 :(得分:1)

import multiprocessing
import subprocess
import shlex
import time

commands = ("echo -n HI-FIRST ", "echo -n HI-SECOND ")
def parallel():
    p = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
    stdoutdata, stderrdata = p.communicate()
    print stdoutdata + "\t" + time.ctime()
for cmd in commands:
    p = multiprocessing.Process(target=parallel)
    p.start()

<强>输出:

$ python stack.py 
HI-FIRST    Fri Jan 11 08:47:18 2013
HI-SECOND   Fri Jan 11 08:47:18 2013