[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

print(text) # methods instead of if/elif logic
def run(self, cmdline):
assert False, 'run must be defined'


class System(LaunchMode):
"""
run Python script named in shell command line
caveat: may block caller, unless & added on Unix
"""
def run(self, cmdline):
cmdline = fixWindowsPath(cmdline)
os.system('%s %s' % (pypath, cmdline))


class Popen(LaunchMode):
"""
run shell command line in a new process
caveat: may block caller, since pipe closed too soon
"""
def run(self, cmdline):
cmdline = fixWindowsPath(cmdline)
os.popen(pypath + ' ' + cmdline) # assume nothing to be read


class Fork(LaunchMode):
"""
run command in explicitly created new process
for Unix-like systems only, including cygwin
"""
def run(self, cmdline):
assert hasattr(os, 'fork')
cmdline = cmdline.split() # convert string to list
if os.fork() == 0: # start new child process
os.execvp(pypath, [pyfile] + cmdline) # run new program in child


class Start(LaunchMode):
"""
run command independent of caller
for Windows only: uses filename associations
"""
def run(self, cmdline):
assert sys.platform[:3] == 'win'
cmdline = fixWindowsPath(cmdline)
os.startfile(cmdline)


class StartArgs(LaunchMode):
"""
for Windows only: args may require real start
forward slashes are okay here
"""
def run(self, cmdline):
assert sys.platform[:3] == 'win'
os.system('start ' + cmdline) # may create pop-up window


class Spawn(LaunchMode):
"""
run python in new process independent of caller


A Portable Program-Launch Framework | 265
Free download pdf