与服务器上存储的 python 个对象交互

Interact with stored python objects on server

我想让 python class 永远活着,这样我就可以不断地与它互动。这样做的原因是这个 class 是高度内存密集型的,这意味着(1)我不能多次将它放入内存,并且(2)加载 class 非常慢。

我已经尝试使用这两个 Pyro and RPYC 来实现它,但看起来这些包总是删除对象并在每次发出新请求时创建一个新对象(这正是我不想要的做。)但是,我确实为 Pyro 找到了以下选项:

@Pyro4.behavior(instance_mode="single")

这确保只创建一个实例。但是,由于可能会同时发出多个请求,因此我不能 100% 确定这样做是安全的。有没有更好的方法来完成我想做的事情?

在此先感谢您的帮助,非常感谢! (我已经为此苦苦挣扎了一段时间)。

L

如果您不想让您的 class 线程安全,您可以将 SERVERTYPE 设置为 "multiplex",这将使所有远程方法调用按顺序处理。

https://pythonhosted.org/Pyro4/servercode.html#server-types-and-concurrency-model:

multiplexed server (servertype "multiplex")

This server uses a connection multiplexer to process all remote method calls sequentially. No threads are used in this server. It uses the best supported selector available on your platform (kqueue, poll, select). It means only one method call is running at a time, so if it takes a while to complete, all other calls are waiting for their turn (even when they are from different proxies). The instance mode used for registering your class, won’t change the way the concurrent access to the instance is done: in all cases, there is only one call active at all times. Your objects will never be called concurrently from different threads, because there are no threads. It does still affect when and how often Pyro creates an instance of your class.