在Python中与os.system()并行运行两个可执行文件?

时间:2016-04-29 16:07:56

标签: python multithreading python-2.7 subprocess

我希望并行运行两个可执行的a.exe和b.exe,一个接一个地调用。

当我尝试时,

os.system('a.exe')
#some code
os.system('b.exe')

b.exe在我杀死a.exe后才开始启动? 为什么会这样? 我怎样才能同时运行? (我需要做多线程吗?) 注意:我在Windows平台

5 个答案:

答案 0 :(得分:3)

如果我们忽略异常,那么同时运行多个程序很简单:

#!/usr/bin/env python
import subprocess

# start all programs
processes = [subprocess.Popen(program) for program in ['a', 'b']]
# wait
for process in processes:
    process.wait()

请参阅Python threading multiple bash subprocesses?

如果要在任何程序无法启动时停止先前启动的进程:

#!/usr/bin/env python3
from contextlib import ExitStack
from subprocess import Popen


def kill(process):
    if process.poll() is None:  # still running
        process.kill()

with ExitStack() as stack:  # to clean up properly in case of exceptions
    processes = []
    for program in ['a', 'b']:
        processes.append(stack.enter_context(Popen(program)))  # start program
        stack.callback(kill, processes[-1])
    for process in processes:
        process.wait()

答案 1 :(得分:2)

尝试将每个作为一个单独的线程运行:

import thread

thread.start_new_thread(os.system, ('a.exe',))
thread.start_new_thread(os.system, ('b.exe',))

答案 2 :(得分:2)

您可能想尝试subprocess.Popen,这允许进程执行但不会阻止。但是,在这种情况下,你必须考虑僵尸进程。

答案 3 :(得分:0)

您可以使用特定方式运行两个或多个命令或程序,例如Python的 线程 库。这里有一个关于它如何工作的广泛例子。

import threading
import time

exitFlag = 0

class myThread (threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
    def run(self):
        print "Starting " + self.name
        print_time(self.name, self.counter, 5)
        print "Exiting " + self.name

def print_time(threadName, delay, counter):
    while counter:
        if exitFlag:
            threadName.exit()
        time.sleep(delay)
        print "%s: %s" % (threadName, time.ctime(time.time()))
        counter -= 1

# Create new threads
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)

# Start new Threads
thread1.start()
thread2.start()

print "Exiting Main Thread"

然后,您的代码可能是这样的:

import threading


class myThread (threading.Thread):
    def __init__(self, command):
        threading.Thread.__init__(self)
        self.cmd = command

    def run(self):
        print "Starting " + self.cmd
        os.system(self.cmd)
        print "Exiting " + self.cmd

lstCmd=["a.exe","b.exe","ping 192.168.0.10","some command"]

# Create new threads
thread1 = myThread(lstCmd[0])
thread2 = myThread(lstCmd[1])
thread3 = myThread(lstCmd[2])
thread4 = myThread(lstCmd[3])

# Start new Threads
thread1.start()
thread2.start()
thread3.start()
thread4.start()

答案 4 :(得分:0)

一个老问题,我自己也是python的新手,但如果你试图与多个/不同的可执行文件同时调用,我发现'Popen'是合适的。

from subprocess import Popen

Popen('a.exe', shell=False)
Popen('b.exe', shell=False)

我发现它在我的用例中比'Threading'(上面的@LaloRamírez示例)更有用,因为Popen(虽然起初很棘手,特别是shell参数)似乎更容易管理,检查和终止进程一旦开始(通过与p1或p2交互,如下例所示)。为了便于阅读,使用别名可能也很有用。

from subprocess import Popen as new
from time import sleep

p1 = new('a.exe', shell=False)
p2 = new('b.exe', shell=False)

sleep(20)

p1.terminate()
p1.wait()

有趣的是'Threading'正在模仿Java的线程功能,这对于那些具有Java多线程经验的人来说可能更合适。 'Popen'方法对我来说似乎更简单。