简体   繁体   English

Django中请求之间的共享对象

[英]Shared object between requests in Django

I am using a Python module ( PyCLIPS ) and Django 1.3. 我正在使用Python模块( PyCLIPS )和Django 1.3。

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. 我想开发一个线程安全类,它实现了Object Pool和Singleton模式,并且还必须在Django中的请求之间共享。

For example, I want to do the following: 例如,我想要做以下事情:

  • A request gets the object with some ID from the pool, do something with it and push it back to the pool, then send response with the object's ID. 请求从池中获取具有某个ID的对象,对其执行某些操作并将其推回池中,然后使用对象的ID发送响应。
  • Another request, that has the object's ID, gets the object with the given ID from the pool and repeats the steps from the above request. 另一个具有对象ID的请求从池中获取具有给定ID的对象,并重复上述请求中的步骤。
  • But the state of the object will has to be kept while it'll be at the pool while the server is running. 但是当服务器运行时,对象的状态必须保留在池中。

It should be like a Singleton Session Bean in Java EE 它应该像Java EE中Singleton Session Bean

How I should do it? 我应该怎么做? Is there something I'll should read? 有什么我应该读的吗?

Update: I can't store objects from the pool in a database, because these objects are wrappers under a library written on C-language which is API for the Expert System Engine CLIPS . 更新:我无法在数据库中存储池中的对象,因为这些对象是在C语言编写的库下的包装器,C语言是专家系统引擎CLIPS的 API。

Thanks! 谢谢!

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不像Java,解决方案应该针对多进程环境而不是多线程环境进行定制。

Django has no immediate equivalent of a singleton session bean. Django没有立即相当于单例会话bean。

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. 您希望保存每个对象数据,该数据应始终位于数据库层中。

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 . 否则,您可以随时在会话中保存内容,Django为登录用户和匿名用户提供 - 请参阅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. 考虑到运行Java Web容器和Python / Django多进程环境之间的巨大差异,使用Java环境中您可能熟悉的任何其他模式最终都会失败。


Edit: 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. 我的直觉是,这些对象不应该由Web层处理,而是由可以从多进程环境访问的某种外部服务处理。 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. 但感觉好像这些对象不属于Web层。

Assuming the object cannot be pickled, you will need to create an app to manage the object and all of the interactions that need to happen against it. 假设无法对对象进行pickle,则需要创建一个应用程序来管理对象以及需要对其进行的所有交互。 Probably the easiest implementation would be to create a single process wsgi app (on a different port) that exposes an api to do all of the operations that you need. 最简单的实现可能是创建一个单独的进程wsgi app(在不同的端口上),它公开了一个api来完成你需要的所有操作。 Whether you use a RESTful api or form posts is up to your personal preference. 无论您使用RESTful api还是表单帖子都取决于您的个人喜好。

Are these database objects? 这些数据库对象是? Because if so, the db itself is really the pool, and there's no need to do anything special - each request can independently load the instance from the db, modify it, and save it back. 因为如果是这样,db本身就是池,并且不需要做任何特殊的事情 - 每个请求都可以独立地从db加载实例,修改它并将其保存回来。

Edit after comment Well, the biggest problem is that a production web server environment is likely to be multi-process, so any global variables (ie the pool) are not shared between processes. 评论后编辑嗯,最大的问题是生产Web服务器环境可能是多进程的,因此任何全局变量(即池)都不会在进程之间共享。 You will need to store them somewhere that's globally accessible. 你需要将它们存储的地方 ,是全局访问。 A short in the dark, but are they serializable using Pickle? 在黑暗中短暂,但是它们是否可以使用Pickle进行序列化? If so, then perhaps memcache might work. 如果是这样,那么也许memcache可能会起作用。

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

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