4

我正在尝试使用以下代码并行执行两个命令 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
4

2 回答 2

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()
于 2013-01-11T02:07:20.773 回答
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
于 2013-01-11T02:50:34.367 回答