0

I have a python memory management problem.

I have a django application running on windows that loads a 2gb model in memory (using dictionary and numpy).

The problem happens when I run this application in the background (using the windows scheduler), after some time without requests, python frees dictionary memory by storing the values ​​on disk.

When I have requests again, python loads these values ​​in memory again, caches again, which ends up increasing the response time of the first requests (normalizing right after, when the values ​​are in memory again).

I wonder if there is any way to force python to keep these variables in cache, so that it does not do this management on its own.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
jvfach
  • 41
  • 4

2 Answers2

0

If your view is just simply displaying the model, cache the view as descripted in the cache doc. You can set the cache time to a large number to prevent cache being cleared frequently like

from django.views.decorators.cache import cache_page

@cache_page(60 * 60 * 24)  # 24 hours
def my_view(request):
    ...

Or if your view does process the data and return values dynamically, for example you want to return everything for admin but only part of the data for non-admin users, you can use the low level cache API to cache the variable(object)

from django.core.cache import cache

# in the script where you get your object the first time
cache.set('my_obj', obj_to_be_cached, 60 * 60 * 24)  # 24 hours

# in your view, retrieve it
def my_view(request):
    cached_obj = cache.get('my_obj')
    ...  # process it
Foocli
  • 157
  • 1
  • 11
0

To cache data, you can use Local-memory caching which you can use without any settings because Local-memory caching is default:

from django.core.cache import cache

def test():
    # Cache data with the key "test"
    cache.set('test', 'Hello World', 120) # 120 seconds (2 minutes)
           
           # Get cache data with the key "test"
    data = cache.get('test')

    print(data) # Hello World
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129