<bdo id='VTYsC'></bdo><ul id='VTYsC'></ul>
      <tfoot id='VTYsC'></tfoot>
    1. <small id='VTYsC'></small><noframes id='VTYsC'>

      <legend id='VTYsC'><style id='VTYsC'><dir id='VTYsC'><q id='VTYsC'></q></dir></style></legend>

      <i id='VTYsC'><tr id='VTYsC'><dt id='VTYsC'><q id='VTYsC'><span id='VTYsC'><b id='VTYsC'><form id='VTYsC'><ins id='VTYsC'></ins><ul id='VTYsC'></ul><sub id='VTYsC'></sub></form><legend id='VTYsC'></legend><bdo id='VTYsC'><pre id='VTYsC'><center id='VTYsC'></center></pre></bdo></b><th id='VTYsC'></th></span></q></dt></tr></i><div id='VTYsC'><tfoot id='VTYsC'></tfoot><dl id='VTYsC'><fieldset id='VTYsC'></fieldset></dl></div>

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

        Calling a python script with input within a python script using subprocess(使用子进程在 python 脚本中调用带有输入的 python 脚本)
      1. <i id='M7fWP'><tr id='M7fWP'><dt id='M7fWP'><q id='M7fWP'><span id='M7fWP'><b id='M7fWP'><form id='M7fWP'><ins id='M7fWP'></ins><ul id='M7fWP'></ul><sub id='M7fWP'></sub></form><legend id='M7fWP'></legend><bdo id='M7fWP'><pre id='M7fWP'><center id='M7fWP'></center></pre></bdo></b><th id='M7fWP'></th></span></q></dt></tr></i><div id='M7fWP'><tfoot id='M7fWP'></tfoot><dl id='M7fWP'><fieldset id='M7fWP'></fieldset></dl></div>

          <bdo id='M7fWP'></bdo><ul id='M7fWP'></ul>

          <tfoot id='M7fWP'></tfoot>

              <legend id='M7fWP'><style id='M7fWP'><dir id='M7fWP'><q id='M7fWP'></q></dir></style></legend>
                  <tbody id='M7fWP'></tbody>

                  <small id='M7fWP'></small><noframes id='M7fWP'>

                  本文介绍了使用子进程在 python 脚本中调用带有输入的 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个脚本 a.py 并且在执行它时会询问用户某些查询并以 json 格式构建输出.使用 python 子进程,我可以从另一个名为 b.py 的脚本中调用此脚本.一切都按预期工作,除了我无法在变量中获得输出?我在 Python 3 中这样做.

                  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 this script from another script named b.py. Everything is working as expected except that I am not able to get the output in a variable? I am doing this in Python 3.

                  推荐答案

                  使用 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='
                  '.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'])
                  

                  这篇关于使用子进程在 python 脚本中调用带有输入的 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                1. <tfoot id='V1t8Z'></tfoot>
                    <bdo id='V1t8Z'></bdo><ul id='V1t8Z'></ul>
                      <tbody id='V1t8Z'></tbody>
                      <legend id='V1t8Z'><style id='V1t8Z'><dir id='V1t8Z'><q id='V1t8Z'></q></dir></style></legend>

                          <small id='V1t8Z'></small><noframes id='V1t8Z'>

                          • <i id='V1t8Z'><tr id='V1t8Z'><dt id='V1t8Z'><q id='V1t8Z'><span id='V1t8Z'><b id='V1t8Z'><form id='V1t8Z'><ins id='V1t8Z'></ins><ul id='V1t8Z'></ul><sub id='V1t8Z'></sub></form><legend id='V1t8Z'></legend><bdo id='V1t8Z'><pre id='V1t8Z'><center id='V1t8Z'></center></pre></bdo></b><th id='V1t8Z'></th></span></q></dt></tr></i><div id='V1t8Z'><tfoot id='V1t8Z'></tfoot><dl id='V1t8Z'><fieldset id='V1t8Z'></fieldset></dl></div>