Python:在同一时间启动多个脚本

2024-06-08 04:18:13 发布

您现在位置:Python中文网/ 问答频道 /正文

此程序启动第一个程序。但我也想跑第二条平行线。 如何用脚本启动两个或多个程序?在

# start many programs
execfile('C:/Dokumente und Einstellungen/schnei17/Desktop/python/zeit/1.py')
print 1
execfile('C:/Dokumente und Einstellungen/schnei17/Desktop/python/zeit/2.py')
print 2

Tags: py程序脚本startmanyprintdesktopzeit
2条回答

尝试使用子进程python模块:

import subprocess
subprocess.Popen(["python.exe", 'C:/Dokumente und Einstellungen/schnei17/Desktop/python/zeit/1.py'])
subprocess.Popen(["python.exe", 'C:/Dokumente und Einstellungen/schnei17/Desktop/python/zeit/2.py'])

它将并行启动两个脚本(如果python.exe在路径中)。在

要启动几个应用程序,我建议使用线程。在

shellcommands=("notepad.exe",
               "calc.exe",
               "mspaint.exe")

import os
import sys
import time
import datetime
import threading
import subprocess

class ThreadClass(threading.Thread):
    # Override Thread's __init__ method to accept the parameters needed:

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

    def run(self):
        now = datetime.datetime.now()
        print "%s %s %s \n" % (self.getName(), self.command,now)
        try:
            subprocess.call(self.command, shell=True)
        except Exception, err:
            print "ERROR: %s\n" % str(err)

for cmd in shellcommands:
    t = ThreadClass(cmd)
    t.start()

sys.exit()

相关问题 更多 >

    热门问题