要啓動幾個應用程序,我建議使用線程。
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()
你爲什麼不使用'subprocess'呢? – 2011-05-10 14:11:25
'subprocess'使得這更容易,除非你需要在進程之間共享數據,在這種情況下,你會使用'多處理' – tMC 2011-05-10 14:14:03