0

尝试使用带有 Spickle 的 stackless python (2.7.2) 通过 celery 发送测试方法,以便在不同的机器上执行。我希望测试方法(代码)包含在 pickle 中,而不是强制存在于执行机器 python 路径上。

一直在参考以下演示文稿: https ://ep2012.europython.eu/conference/talks/advanced-pickling-with-stackless-python-and-spickle

尝试使用检查点幻灯片 11 中显示的技术。鉴于我们使用的是 celery,RPC 示例似乎不正确:

客户端代码:

from stackless import run, schedule, tasklet
from sPickle import SPickleTools


def test_method():
    print "hello from test method"

tasks = []
test_tasklet = tasklet(test_method)()
tasks.append(test_tasklet)

pt = SPickleTools(serializeableModules=['__test_method__'])
pickled_task = pt.dumps(tasks)

服务器代码:

pt = sPickle.SPickleTools()
unpickledTasks = pt.loads(pickled_task)

结果是:

[2012-03-09 14:24:59,104: ERROR/MainProcess] Task    
celery_tasks.test_exec_method[8f462bd6-7952-4aa1-9adc-d84ee4a51ea6] raised exception:   
AttributeError("'module'
object has no attribute 'test_method'",)
Traceback (most recent call last):
File "c:\Python27\lib\site-packages\celery\execute\trace.py", line 153, in trace_task
R = retval = task(*args, **kwargs)
File "c:\Python27\celery_tasks.py", line 16, in test_exec_method
unpickledTasks = pt.loads(pickled_task)
File "c:\Python27\lib\site-packages\sPickle\_sPickle.py", line 946, in loads
return unpickler.load()
AttributeError: 'module' object has no attribute 'test_method'

关于我做错了什么或者这是否可能的任何建议?

在 celeryd 中进行动态模块加载的替代建议也很好(作为使用 sPickle 的替代方法)。我已经尝试过这样做:

py_mod = imp.load_source(module_name,'some script path')
sys.modules.setdefault(module_name,py_mod)

但是动态加载的模块似乎并没有通过对 celeryd 的不同调用(即不同的远程调用)持续存在。

4

1 回答 1

1

您必须test_method在其自己的模块中定义。目前 sPickle 检测是否 test_method定义在可以导入的模块中。另一种方法是将__module__函数的属性设置为None.

def test_method():
    pass

test_method.__module__ = None
于 2012-06-24T10:17:10.927 回答