Shared object between requests in Django

后端 未结 3 409
臣服心动
臣服心动 2020-12-19 21:06

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

I want develop a thread-safety class which realizes the Object Pool and the Singleton patterns and also that hav

相关标签:
3条回答
  • 2020-12-19 21:45

    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. 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. Whether you use a RESTful api or form posts is up to your personal preference.

    0 讨论(0)
  • 2020-12-19 21:59

    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 has no immediate equivalent of a singleton session 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.

    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.


    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. 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.

    0 讨论(0)
  • 2020-12-19 22:02

    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.

    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. You will need to store them somewhere that's globally accessible. A short in the dark, but are they serializable using Pickle? If so, then perhaps memcache might work.

    0 讨论(0)
提交回复
热议问题