Asynchronous task queue processing of in-memory data stucture in Django

*爱你&永不变心* 提交于 2019-12-08 05:00:43

问题


I have a singleton in-memory data-structure inside my Django project (some kind of kd-tree that needs to get accessed all across the project).

For those that don't know Django, I believe the same issue would appear with a regular Python code.

I know it's evil (Singleton), and I'm looking for better ways to implement that, but my question here is related to another topic:

I am instantiating the singleton inside my code by calling Singleton.instance() and it gives me the object correctly, it then stays at some place in the memory inside my ./manage.py runserver.

The problem is that I am making some asynchronous processing with Celery on this same Singleton data-structure (such as reconstructing the kd-tree).

BUT when launching a Celery worker, it runs the code within a different process, and therefore has a different memory space, which means that it works on a totally different instance of the Singleton.

What would be the best Design Pattern to this issue? I have thought of doing all the processing related to my data-structure inside the Django project (without using Celery) but what I liked very much about Celery is that the processing required on the data-structure can take a long-time (around 30 seconds) and it needs to handle concurrency nicely (there could be several requests at the same time to reconstruct the Kd-Tree).

I would be very glad to have some insights for this since I'm not making any progress those last 3 days. Thanks a lot.

来源:https://stackoverflow.com/questions/26088868/asynchronous-task-queue-processing-of-in-memory-data-stucture-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!