- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
根据 StackOverflow 上的许多建议(例如 dill、cloudpickle 等),我尝试了多种方法来 pickle 带有依赖项的 python 函数,但所有方法似乎都遇到了一个我无法弄清楚的基本问题。
我有一个主模块试图从一个导入的模块中 pickle 一个函数,通过 ssh 发送它以在远程机器上进行 unpickled 和执行。
所以 main 有:
import dill (for example)
import modulea
serial=dill.dumps( modulea.func )
send (serial)
在远程机器上:
import dill
receive serial
funcremote = dill.loads( serial )
funcremote()
如果被 pickle 和发送的函数是 main 本身定义的顶级函数,那么一切正常。当它们在导入的模块中时,加载函数失败并显示“找不到模块模块”类型的消息。
模块名称似乎与函数名称一起被 pickle。我没有看到任何方法来“修复”pickle 以删除依赖项,或者在接收器中创建一个虚拟模块以成为 unpickling 的接收者。
任何指点将不胜感激。
--普拉萨纳
最佳答案
我是 dill
的作者。我通过 ssh
做了这件事,但成功了。目前,dill
和任何其他序列化程序通过引用来 pickle 模块……因此要成功传递文件中定义的函数,您必须确保相关模块也安装在另一台机器上。我不相信有任何对象序列化程序可以直接序列化模块(即不通过引用)。
话虽如此,dill
确实有一些选项可以序列化对象依赖项。例如,对于类实例,dill
中的默认设置是不通过引用序列化类实例……因此类定义也可以序列化并与实例一起发送。在 dill
中,您还可以(使用一个非常新的功能)通过序列化文件来序列化文件句柄,而不是通过引用这样做。但是同样,如果您遇到在模块中定义函数的情况,那您就不走运了,因为模块是通过引用序列化的,非常普遍。
您也许可以使用 dill
来这样做,但是,只是不对对象进行 pickle ,而是提取源代码并发送源代码。在pathos.pp
和pyina
中,我们使用dill
提取源 和依赖项 任何对象(包括函数),并将它们传递给另一台计算机/进程/等。然而,由于这不是一件容易的事,dill
也可以使用尝试提取相关导入并发送它而不是源代码的故障转移。
希望您能理解,这是一件很麻烦的事情(正如我在下面提取的函数的一个依赖项中所指出的)。但是,您所要求的是在 pathos
包中成功完成,以通过 ssh 隧道端口将代码和依赖项传递到不同的机器。
>>> import dill
>>>
>>> print dill.source.importable(dill.source.importable)
from dill.source import importable
>>> print dill.source.importable(dill.source.importable, source=True)
def _closuredsource(func, alias=''):
"""get source code for closured objects; return a dict of 'name'
and 'code blocks'"""
#FIXME: this entire function is a messy messy HACK
# - pollutes global namespace
# - fails if name of freevars are reused
# - can unnecessarily duplicate function code
from dill.detect import freevars
free_vars = freevars(func)
func_vars = {}
# split into 'funcs' and 'non-funcs'
for name,obj in list(free_vars.items()):
if not isfunction(obj):
# get source for 'non-funcs'
free_vars[name] = getsource(obj, force=True, alias=name)
continue
# get source for 'funcs'
#…snip… …snip… …snip… …snip… …snip…
# get source code of objects referred to by obj in global scope
from dill.detect import globalvars
obj = globalvars(obj) #XXX: don't worry about alias?
obj = list(getsource(_obj,name,force=True) for (name,_obj) in obj.items())
obj = '\n'.join(obj) if obj else ''
# combine all referred-to source (global then enclosing)
if not obj: return src
if not src: return obj
return obj + src
except:
if tried_import: raise
tried_source = True
source = not source
# should never get here
return
我想也可以围绕 dill.detect.parents
方法构建一些东西,该方法提供指向任何给定对象的所有父对象的指针列表……并且可以重建所有函数的依赖项作为对象……但这没有实现。
顺便说一句:要建立 ssh 隧道,只需这样做:
>>> t = pathos.Tunnel.Tunnel()
>>> t.connect('login.university.edu')
39322
>>> t
Tunnel('-q -N -L39322:login.university.edu:45075 login.university.edu')
然后您可以使用 ZMQ
或 ssh
或其他任何方式跨本地端口工作。如果你想用 ssh
这样做,pathos
也有内置的。
关于python - 序列化具有依赖项的 python 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26389981/
这是代码片段。 请说出这种用小内存存储大数据的算法是什么。 public static void main(String[] args) { long longValue = 21474836
所以我使用 imap 从 gmail 和 outlook 接收电子邮件。 Gmail 像这样编码 =?UTF-8?B?UmU6IM69zq3OvyDOtc68zrHOuc67IG5ldyBlbWFpb
很久以前就学会了 C 代码;想用 Scheme 尝试一些新的和不同的东西。我正在尝试制作一个接受两个参数并返回两者中较大者的过程,例如 (define (larger x y) (if (> x
Azure 恢复服务保管库有两个备份配置选项 - LRS 与 GRS 这是一个有关 Azure 恢复服务保管库的问题。 当其驻留区域发生故障时,如何处理启用异地冗余的恢复服务保管库?如果未为恢复服务启
说,我有以下实体: @Entity public class A { @Id @GeneratedValue private Long id; @Embedded private
我有下一个问题。 我有下一个标准: criteria.add(Restrictions.in("entity.otherEntity", getOtherEntitiesList())); 如果我的
如果这是任何类型的重复,我会提前申请,但我找不到任何可以解决我的具体问题的内容。 这是我的程序: import java.util.Random; public class CarnivalGame{
我目前正在使用golang创建一个聚合管道,在其中使用“$ or”运算符查询文档。 结果是一堆需要分组的未分组文档,这样我就可以进入下一阶段,找到两个数据集之间的交集。 然后将其用于在单独的集合中进行
是否可以在正则表达式中创建 OR 条件。 我正在尝试查找包含此类模式的文件名列表的匹配项 第一个案例 xxxxx-hello.file 或者案例二 xxxx-hello-unasigned.file
该程序只是在用户输入行数时创建菱形的形状,因此它有 6 个 for 循环; 3 个循环创建第一个三角形,3 个循环创建另一个三角形,通过这 2 个三角形和 6 个循环,我们得到了一个菱形,这是整个程序
我有一个像这样的查询字符串 www.google.com?Department=Education & Finance&Department=Health 我有这些 li 标签,它们的查询字符串是这样
我有一个带有静态构造函数的类,我用它来读取 app.config 值。如何使用不同的配置值对类进行单元测试。我正在考虑在不同的应用程序域中运行每个测试,这样我就可以为每个测试执行静态构造函数 - 但我
我正在寻找一个可以容纳多个键的容器,如果我为其中一个键值输入保留值(例如 0),它会被视为“或”搜索。 map, int > myContainer; myContainer.insert(make_
我正在为 Web 应用程序创建数据库,并正在寻找一些建议来对可能具有多种类型的单个实体进行建模,每种类型具有不同的属性。 作为示例,假设我想为“数据源”对象创建一个关系模型。所有数据源都会有一些共享属
(1) =>CREATE TABLE T1(id BIGSERIAL PRIMARY KEY, name TEXT); CREATE TABLE (2) =>INSERT INTO T1 (name)
我不确定在使用别名时如何解决不明确的列引用。 假设有两个表,a 和 b,它们都有一个 name 列。如果我加入这两个表并为结果添加别名,我不知道如何为这两个表引用 name 列。我已经尝试了一些变体,
我的查询是: select * from table where id IN (1,5,4,3,2) 我想要的与这个顺序完全相同,不是从1...5,而是从1,5,4,3,2。我怎样才能做到这一点? 最
我正在使用 C# 代码执行动态生成的 MySQL 查询。抛出异常: CREATE TABLE dump ("@employee_OID" VARCHAR(50)); "{"You have an er
我有日期 2016-03-30T23:59:59.000000+0000。我可以知道它的格式是什么吗?因为如果我使用 yyyy-MM-dd'T'HH:mm:ss.SSS,它会抛出异常 最佳答案 Sim
我有一个示例模式,它的 SQL Fiddle 如下: http://sqlfiddle.com/#!2/6816b/2 这个 fiddle 只是根据 where 子句中的条件查询示例数据库,如下所示:
我是一名优秀的程序员,十分优秀!