我想知道从已经运行的脚本中执行另一个Python脚本的执行是否是一种常见做法?
我说subprocesss.Pipe()是一个选择,如果它用于运行一些外部进程(例如:其他应用程序或系统命令)。至于"虚拟"子进程当前运行的脚本的方法(或函数)multitreamnent将完全有意义。
答案 0 :(得分:2)
从不同文件运行Python代码的常用方法是将其作为模块导入并直接使用其中定义的对象:
# assert "/filepath/to" in sys.path
import your_script # import module defined in your_script.py
your_script.copyfile(srcfile, dest)
要在多个进程中运行Python代码,可以使用multiprocessing
module:
from multiprocessing import Pool
import your_script
def copyfile(srcfile):
try:
dest = ... srcfile ...
your_script.copyfile(srcfile, dest)
except Exception as e:
return srcfile, None, str(e)
else:
return srcfile, dest, None # no errors
def main():
sources = [...]
pool = Pool(20) # don't copy more that 20 files at once
results = pool.map(copyfile, sources)
if __name__ == "__main__":
main()
要使用线程而不是进程,只需更改导入:
from multiprocessing.dummy import Pool # use threads
目前尚不清楚性能并行IO操作可能会产生什么影响。
答案 1 :(得分:0)
import os, sys, subprocess
from multiprocessing import Pool
def copyUsingSubprocess(source):
folder=os.path.dirname(source)
srcFileName, srcFileExt=os.path.splitext(os.path.basename(source))
destFilename=srcFileName+'_dest'+srcFileExt
dest='/'.join([folder,destFilename])
cmd=['cp', source, dest]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sources = ['/Volumes/files/a.data','/Volumes/files/b.data',
'/Volumes/files/c.data','/Volumes/files/d.data',
'/Volumes/files/e.data','/Volumes/files/f.data']
pool = Pool(20)
results = pool.map(copyUsingSubprocess, sources)
import os, sys, subprocess
def copyUsingSubprocess(cmd):
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
sources = ['/Volumes/files/a.data','/Volumes/files/b.data',
'/Volumes/files/c.data','/Volumes/files/d.data',
'/Volumes/files/e.data','/Volumes/files/f.data']
for source in sources:
folder=os.path.dirname(source)
srcFileName, srcFileExt=os.path.splitext(os.path.basename(source))
destFilename=srcFileName+'_dest'+srcFileExt
dest='/'.join([folder,destFilename])
cmd=['cp', source, dest]
copyUsingSubprocess(cmd)