获取" ImportError:没有名为&#34的模块;使用并行python和包中的方法

时间:2014-10-06 06:50:52

标签: python parallel-python dill

我尝试使用并行python来进行分布式基准测试(本质上,从中央服务器协调并运行一组机器上的代码)。我的代码工作得非常好,直到我将功能移到一个单独的包中。从那时起,我不断获得ImportError: No module named some.module.pp_test

我的问题实际上是双重问题:有没有人遇到pp这个问题,如果是的话,如何解决?我尝试使用dillimport dill),但没有帮助。此外,是否有一个很好的替代parallelpython,不需要任何额外的基础设施?

我得到的确切错误是:

RUNNING TEST
Waiting for hosts to finish booting....A fatal error has occured during the function execution
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/ppworker.py", line 86, in run
    __args = pickle.loads(__sargs)
ImportError: No module named some.module.pp_test
Caught exception in the run phase 'NoneType' object is not iterable
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    p.ping_pong()
  File "/home/ubuntu/workspace/pp-test/some/module/pp_test.py", line 5, in ping_pong
    a_test.run()
  File "/home/ubuntu/workspace/pp-test/some/module/pp_test.py", line 27, in run
    pong, hostname = ping()
TypeError: 'NoneType' object is not iterable

代码以这种方式构建:

pp-test/
       test.py
       some/
            __init__.py
            module/
                   __init__.py
                   pp_test.py

test.py实现为:

from some.module.pp_test import MWE

p = MWE()
p.ping_pong()

虽然pp_test.py是:

class MWE():
  def ping_pong(self):
    print "RUNNING TEST "
    a_test = PPTester()
    a_test.run()

import pp
import time
from sys import stdout, exit

class PPTester(object):
  def run(self):
    try:
        ppservers = ('10.10.10.10', )
        time.sleep(5)
        job_server = pp.Server(0, ppservers=ppservers)
        stdout.write("Waiting for hosts to finish booting...")
        while len(job_server.get_active_nodes()) - 1 < len(ppservers):
            stdout.write(".")
            stdout.flush()
            time.sleep(1)

        ppmodules = ()
        pings = [(server, job_server.submit(self.run_pong, modules=ppmodules)) for server in ppservers]
        for server, ping in pings:
            pong, hostname = ping()
            print "Host ", hostname, " is alive!"

        print "All servers booted up, starting benchmarks..."
        job_server.print_stats()
    except Exception as e:
        print "Caught exception in the run phase", e
        raise
    pass

  def run_pong(self):
    import subprocess
    p = subprocess.Popen("hostname", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    p_status = p.wait()

    return "pong ", output

1 个答案:

答案 0 :(得分:0)

dill无法使用pp开箱即用,因为pp没有对python对象进行序列化 - pp提取对象& #39;源代码(如标准python库中的inspect模块)。

要让pp能够使用dill(实际为dill.sourceinspect增强dill),您必须使用pp的分叉1}}调用ppftppft安装为pp(即使用import pp导入),但它具有更强大的源检查功能,因此您可以自动&#34;序列化&#34;大多数python对象并让ppft自动跟踪它们的依赖关系。

在此处获取ppfthttps://github.com/uqfoundation

ppft也可pip可安装且与3.x兼容。