在Django中的请求之间共享对象 [英] Shared object between requests in Django

查看:211
本文介绍了在Django中的请求之间共享对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python模块( PyCLIPS )和Django 1.3。

I am using a Python module (PyCLIPS) and Django 1.3.

我想开发线程安全类,实现对象池和单例模式,并且必须在Django中的请求之间共享。

I want develop a thread-safety class which realizes the Object Pool and the Singleton patterns and also that have to be shared between requests in Django.

例如,我想要执行以下操作:

For example, I want to do the following:


  • 请求从池中获取一些ID的对象,执行
    与它的东西,并将其推回到池,然后发送响应
    与对象的ID。

  • 具有对象ID的另一个请求从池中获取
    具有给定ID的对象,并重复上述请求中的步骤。

  • 但是当服务器运行时,对象的状态必须保留在池中。

它应该像 Java EE中的单例会话Bean

我应该怎么做?有没有什么我应该读的?

How I should do it? Is there something I'll should read?

更新:
我不能从数据库中的池中存储对象,因为这些对象是以C语言编写的库,是专家系统引擎CLIPS

谢谢!

推荐答案

这里需要角度。 Django不像Java,解决方案应该针对多进程环境进行定制,而不是多线程的。

Well, I think a different angle is necessary here. Django is not like Java, the solution should be tailored for a multi-process environment, not a multi-threaded one.

Django没有立即等同于单例会话bean

Django has no immediate equivalent of a singleton session bean.

说,我看不出你的描述不符合经典数据库模型的原因。您要保存每个对象数据,这些数据应该始终位于DB层中。

That said, I see no reason your description does not fit a classic database model. You want to save per object data, which should always go in the DB layer.

否则,您可以随时保存会话中的东西,Django为登录在用户以及匿名用户中 - 查看Django会话中的文档

Otherwise, you can always save stuff on the session, which Django provides for both logged-in users as well as for anonymous ones - see the docs on Django sessions.

从Java环境可能熟悉的任何其他模式的使用将最终失败,考虑到运行Java Web容器与Python / Django多进程环境。

Usage of any other pattern you might be familiar with from a Java environment will ultimately fail, considering the vast difference between running a Java web container, and the Python/Django multi-process environment.

编辑:您的应用程序通过第三方库访问,它确实使事情变得复杂。我的直觉是,这些对象不应该被Web层处理,而是由某种可以从多进程环境访问的外部服务来处理。正如丹尼尔所说,你可以随时把它们放在缓存中(如果所说的对象是可以打开的)。但是觉得这些对象不属于Web层。

well, considering these objects are not native to your app rather accessed via a third-party library, it does complicate things. My gut feeling is that these objects should not be handled by the web layer but rather by some sort of external service which you can access from a multi-process environment. As Daniel mentioned, you can always throw them in the cache (if said objects are pickle-able). But it feels as if these objects do not belong in the web tier.

这篇关于在Django中的请求之间共享对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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