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

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

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

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

        交互式 Python 中的多处理包

        时间:2023-05-25
        <legend id='FNk1p'><style id='FNk1p'><dir id='FNk1p'><q id='FNk1p'></q></dir></style></legend>

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

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

                  <bdo id='FNk1p'></bdo><ul id='FNk1p'></ul>
                  <tfoot id='FNk1p'></tfoot>
                    <tbody id='FNk1p'></tbody>
                • 本文介绍了交互式 Python 中的多处理包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有以下代码test.py:

                  I have the following code test.py:

                  #multiprocessing in the interactive Python 
                  
                  import time
                  from multiprocessing import Process, Pipe
                  
                  def MyProcess(a):
                  
                      while(1):
                         time.sleep(1)
                         a.send("tic")    
                  
                  if __name__ == "__main__":
                  
                      a, b = Pipe() 
                  
                      p = Process(target=MyProcess, args=(a,))
                      p.start()
                  
                      while(1):
                         msg=b.recv()
                         print(msg)
                  

                  如果我在 DOS shell "python test.py" 中执行它就可以正常工作但如果我使用 IEP (Pyzo) 中的执行文件",它就不起作用.

                  It works fine if I execute it in the DOS shell "python test.py" But it doesn't work if I use "Execute File" from IEP (Pyzo).

                  Traceback (most recent call last):
                    File "<string>", line 1, in <module>
                    File "C:pyzo2014a_64blibmultiprocessingspawn.py", line 106, in spawn_main
                      exitcode = _main(fd)
                    File "C:pyzo2014a_64blibmultiprocessingspawn.py", line 116, in _main
                      self = pickle.load(from_parent)
                  AttributeError: Can't get attribute 'MyProcess' on <module '__main__' (built-in)>
                  

                  我发现这是一个记录在案的问题".请检查以下链接的答案.

                  I found that this is a documented 'issue'. Please check the answer of the link below.

                  交互模式下的多处理中断

                  这是否意味着我不应该使用交互式 Python 中的多处理包?这是否意味着我无法从 IPython 控制台创建进程?对此的任何澄清将不胜感激

                  Does it mean that I should not use multiprocessing package from the interactive Python? Does it mean I can not create a process from the IPython console? Any clarification on this will be highly appreciated

                  推荐答案

                  正确,你不能从解释器中使用 multiprocessing... 主要是因为 pickle 不知道如何序列化交互功能.然而,如果你使用一个名为 pathos.multiprocessingmultiprocessing 分支,你可以从解释器中做你想做的事.这是因为 pathos.multiprocessing 使用 dill,而 dill 知道如何序列化解释器中定义的函数(和其他对象).

                  Correct, you can't use multiprocessing from the interpreter… primarily because pickle doesn't know how to serialize interactive functions. However, if you use a multiprocessing fork, called pathos.multiprocessing, you can do what you want from the interpreter. This works because pathos.multiprocessing uses dill, and dill knows how to serialize functions (and other objects) that are defined in the interpreter.

                  >>> from pathos.multiprocessing import ProcessingPool as Pool
                  >>> 
                  >>> p = Pool(4)
                  >>> def squared(x):
                  ...   return x**2
                  ... 
                  >>> def pow(x,y):
                  ...   return x**y
                  ... 
                  >>> a = range(10)
                  >>> b = range(10,0,-1)
                  >>> 
                  >>> p.map(squared, a)
                  [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
                  >>> res = p.amap(pow, a, b)
                  >>> print "asynchronous, and with multiple inputs!"
                  asynchronous, and with multiple inputs!
                  >>> res.get()
                  [0, 1, 256, 2187, 4096, 3125, 1296, 343, 64, 9]
                  

                  在此处获取 pathos:https://github.com/uqfoundation

                  这篇关于交互式 Python 中的多处理包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python multiprocessing.Pool() 不会使用 100% 的每个 CPU 下一篇:Windows上的python多处理

                  相关文章

                  最新文章

                  <small id='7902v'></small><noframes id='7902v'>

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