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



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

当我尝试时,

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

b.exe是在我杀死a.exe之后才开始的?为什么会发生这种情况?如何同时运行两者?(我需要执行多线程吗?)注意:我在Windows平台

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

#!/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线程化多个bash子流程?

如果您想在任何程序无法启动的情况下停止以前启动的进程:

#!/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()

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

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

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

您可以使用特定的方式来运行两个或多个命令或程序,例如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()

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

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

我发现它在我的用例中比"线程"(上面的@Lalo Ramí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()

有趣的是,"线程"是在模仿Java的线程功能,这可能更适合那些有Java多线程经验的人。"Popen"方法对我来说似乎是一个更简单的选择。

相关内容

  • 没有找到相关文章

最新更新