我们需要腌任何可腌的 [英] We need to pickle any sort of callable

查看:75
本文介绍了我们需要腌任何可腌的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,有人提出了一些Python代码试图通过使用腌制过程来促进分布式计算的问题。显然,该功能在历史上是可能的,但是出于安全原因,该功能被禁用。第二次尝试通过套接字传输功能对象时,仅传输了引用。如果我错了,请纠正我,但我不认为此问题与Python的后期绑定有关。假定不能腌制进程和线程对象,是否有任何方法可以传输可调用对象?我们希望避免为每个作业传输压缩的源代码,因为这可能会使整个尝试变得毫无意义。出于可移植性的原因,只能使用Python核心库。

Recently a question was posed regarding some Python code attempting to facilitate distributed computing through the use of pickled processes. Apparently, that functionality has historically been possible, but for security reasons the same functionality is disabled. On the second attempted at transmitting a function object through a socket, only the reference was transmitted. Correct me if I am wrong, but I do not believe this issue is related to Python's late binding. Given the presumption that process and thread objects can not be pickled, is there any way to transmit a callable object? We would like to avoid transmitting compressed source code for each job, as that would probably make the entire attempt pointless. Only the Python core library can be used for portability reasons.

推荐答案

您可以编组字节码并腌制其他函数: / p>

You could marshal the bytecode and pickle the other function things:

import marshal
import pickle

marshaled_bytecode = marshal.dumps(your_function.func_code)
# In this process, other function things are lost, so they have to be sent separated.
pickled_name = pickle.dumps(your_function.func_name)
pickled_arguments = pickle.dumps(your_function.func_defaults)
pickled_closure = pickle.dumps(your_function.func_closure)
# Send the marshaled bytecode and the other function things through a socket (they are byte strings).
send_through_a_socket((marshaled_bytecode, pickled_name, pickled_arguments, pickled_closure))

在另一个python程序中:

In another python program:

import marshal
import pickle
import types

# Receive the marshaled bytecode and the other function things.
marshaled_bytecode, pickled_name, pickled_arguments, pickled_closure = receive_from_a_socket()
your_function = types.FunctionType(marshal.loads(marshaled_bytecode), globals(), pickle.loads(pickled_name), pickle.loads(pickled_arguments), pickle.loads(pickled_closure))

并且必须在以下位置重新创建对函数内部全局变量的引用

And any references to globals inside the function would have to be recreated in the script that receives the function.

在Python 3中,使用的函数属性为 __ code __ __ name __ __ defaults __ __ closure __

In Python 3, the function attributes used are __code__, __name__, __defaults__ and __closure__.

请注意, send_through_a_socket receive_from_a_socket 实际上并不存在,应将它们替换为通过套接字传输数据的实际代码。

Please do note that send_through_a_socket and receive_from_a_socket do not actually exist, and you should replace them by actual code that transmits data through sockets.

这篇关于我们需要腌任何可腌的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆