Calling a python script with input within a python script using subprocess

前端 未结 2 737
暖寄归人
暖寄归人 2020-11-22 15:32

I have a script a.py and while executing it will ask certain queries to user and frame the output in json format. Using python subprocess, I am able to call thi

2条回答
  •  逝去的感伤
    2020-11-22 16:06

    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)
    

    where get_script_dir() function is defined here.

    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']]
    

    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'])
    

提交回复
热议问题