简体   繁体   English

如何序列化 Python 中的动态 class 实例?

[英]How can you serialize dynamic class instances in Python?

I have a wsdl that takes more than two minutes to parse with Suds.我有一个 wsdl 需要两分钟以上才能用 Suds 解析。 This is too long because I want to use a client object in a short lived process.这太长了,因为我想在短暂的过程中使用客户端 object。 I'd like to persist the client object as a static resource to avoid the overhead of creating a client each time.我想将客户端 object 保留为 static 资源,以避免每次创建客户端的开销。 A few people have suggested a sort of singleton pattern to store the client object in a running process, but I want a short lived process.一些人建议使用一种singleton模式将客户端 object 存储在正在运行的进程中,但我想要一个短暂的进程。 I'm also not looking for an explanation about why pickling dynamically created classes doesn't work, so only post if you have a solution.我也不是在寻找关于 为什么酸洗动态创建的类不起作用的解释,所以只有在你有解决方案的情况下才发布。

from suds.client import Client

url = 'http://mssoapinterop.org/asmx/simple.asmx?WSDL'
client = Client(url)
print client

Since pickling won't work, is there some way to grab the exact bytes for the client object out of memory using the address location, store it as a binary blob, and then load them back into memory later?由于酸洗不起作用,是否有某种方法可以使用地址位置从 memory 中获取客户端 object 的确切字节,将其存储为二进制 blob,然后稍后将它们加载回 ZCD69B4957F06CD818D7BF3D61980? Maybe using ctypes ?也许使用ctypes

Are you asking a question that is specific to the suds library.您是否在问一个特定于 suds 库的问题。 It would be helpful if you can abstract the question to a generic python question not related to a particular library.如果您可以将问题抽象为与特定库无关的通用 python 问题,将会很有帮助。

Assuming you are only interested in the data in the output of the client call which contains values returns from an HTTP call.... why not translate the returned data into your own class format that is not dynamic and then serialize / pickle that.假设您只对客户端调用的 output 中的数据感兴趣,其中包含从 HTTP 调用返回的值......为什么不将返回的数据转换为您自己的 class 而不是动态的序列化/pickle1DZ 格式。

If you are trying to serialize the state of some TCP connection that seems awkard anyway.如果您尝试序列化 state 的某些 TCP 连接,无论如何这似乎很尴尬。

Perhaps you can clarify.或许你可以澄清一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM