且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用子进程在 python 脚本中使用输入调用 python 脚本

更新时间:2023-12-05 21:40:40

使用 subprocess 模块从另一个脚本调用 Python 脚本并传递一些输入并获取其输出:

To call a Python script from another one using subprocess module and to pass it some input and to get its output:

#!/usr/bin/env python3
import os
import sys
from subprocess import check_output

script_path = os.path.join(get_script_dir(), 'a.py')
output = check_output([sys.executable, script_path],
                      input='\n'.join(['query 1', 'query 2']),
                      universal_newlines=True)

get_script_dir() 函数在此处定义.

where get_script_dir() function is defined here.

更灵活的替代方法是导入模块 a 并调用一个函数,以获取结果(确保 a.py 使用 if __name__=="__main__" 保护,避免在导入时运行不需要的代码):

A more flexible alternative is to import module a and to call a function, to get the result (make sure a.py uses if __name__=="__main__" guard, to avoid running undesirable code on import):

#!/usr/bin/env python
import a # the dir with a.py should be in sys.path

result = [a.search(query) for query in ['query 1', 'query 2']]

您可以使用 mutliprocessing 在单独的进程中运行每个查询(如果执行查询是 CPU 密集型的,那么它可能会提高时间性能):

You could use mutliprocessing to run each query in a separate process (if performing a query is CPU-intensive then it might improve time performance):

#!/usr/bin/env python
from multiprocessing import freeze_support, Pool
import a

if __name__ == "__main__":
   freeze_support()
   pool = Pool() # use all available CPUs
   result = pool.map(a.search, ['query 1', 'query 2'])