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

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

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

      1. 是否可以在子进程中运行函数而无需线程或编写单独的文件/脚本.

        Is it possible to run function in a subprocess without threading or writing a separate file/script.(是否可以在子进程中运行函数而无需线程或编写单独的文件/脚本.)
      2. <tfoot id='UmVfF'></tfoot>
          1. <legend id='UmVfF'><style id='UmVfF'><dir id='UmVfF'><q id='UmVfF'></q></dir></style></legend>
                <bdo id='UmVfF'></bdo><ul id='UmVfF'></ul>
                  <i id='UmVfF'><tr id='UmVfF'><dt id='UmVfF'><q id='UmVfF'><span id='UmVfF'><b id='UmVfF'><form id='UmVfF'><ins id='UmVfF'></ins><ul id='UmVfF'></ul><sub id='UmVfF'></sub></form><legend id='UmVfF'></legend><bdo id='UmVfF'><pre id='UmVfF'><center id='UmVfF'></center></pre></bdo></b><th id='UmVfF'></th></span></q></dt></tr></i><div id='UmVfF'><tfoot id='UmVfF'></tfoot><dl id='UmVfF'><fieldset id='UmVfF'></fieldset></dl></div>

                    <tbody id='UmVfF'></tbody>

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

                  本文介绍了是否可以在子进程中运行函数而无需线程或编写单独的文件/脚本.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  import subprocess
                  
                  def my_function(x):
                      return x + 100
                  
                  output = subprocess.Popen(my_function, 1) #I would like to pass the function object and its arguments
                  print output 
                  #desired output: 101
                  

                  我只找到了有关使用单独脚本打开子进程的文档.有谁知道如何传递函数对象,甚至是传递函数代码的简单方法?

                  I have only found documentation on opening subprocesses using separate scripts. Does anyone know how to pass function objects or even an easy way to pass function code?

                  推荐答案

                  我认为您正在寻找更像多处理模块的东西:

                  I think you're looking for something more like the multiprocessing module:

                  http://docs.python.org/library/multiprocessing.html#the-process-class

                  子进程模块用于生成进程并使用它们的输入/输出执行操作 - 不用于运行函数.

                  The subprocess module is for spawning processes and doing things with their input/output - not for running functions.

                  这是您的代码的 multiprocessing 版本:

                  Here is a multiprocessing version of your code:

                  from multiprocessing import Process, Queue
                  
                  # must be a global function    
                  def my_function(q, x):
                      q.put(x + 100)
                  
                  if __name__ == '__main__':
                      queue = Queue()
                      p = Process(target=my_function, args=(queue, 1))
                      p.start()
                      p.join() # this blocks until the process terminates
                      result = queue.get()
                      print result
                  

                  这篇关于是否可以在子进程中运行函数而无需线程或编写单独的文件/脚本.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 画布滚动条?)

                  <small id='1H5Be'></small><noframes id='1H5Be'>

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

                          <tbody id='1H5Be'></tbody>
                          • <bdo id='1H5Be'></bdo><ul id='1H5Be'></ul>
                            <tfoot id='1H5Be'></tfoot>