栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我们需要腌任何可腌的

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我们需要腌任何可腌的

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

import marshalimport picklemarshaled_bytepre = marshal.dumps(your_function.func_pre)# 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 bytepre and the other function things through a socket (they are byte strings).send_through_a_socket((marshaled_bytepre, pickled_name, pickled_arguments, pickled_closure))

在另一个python程序中:

import marshalimport pickleimport types# Receive the marshaled bytepre and the other function things.marshaled_bytepre, pickled_name, pickled_arguments, pickled_closure = receive_from_a_socket()your_function = types.FunctionType(marshal.loads(marshaled_bytepre), globals(), pickle.loads(pickled_name), pickle.loads(pickled_arguments), pickle.loads(pickled_closure))

并且必须在接收该函数的脚本中重新创建该函数内部对全局变量的任何引用。

在Python 3,功能属性使用的是

__pre__
__name__
__defaults__
__closure__

请大家注意,

send_through_a_socket
receive_from_a_socket
实际上不存在的,你应该通过实际的代码通过插座替换它们传送数据。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624332.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号