python threadsafe object cache

前端 未结 6 947
感动是毒
感动是毒 2020-12-29 09:29

I have implemented a python webserver. Each http request spawns a new thread. I have a requirement of caching objects in memory and since its a webserver, I want the cache t

6条回答
  •  失恋的感觉
    2020-12-29 10:01

    Well a lot of operations in Python are thread-safe by default, so a standard dictionary should be ok (at least in certain respects). This is mostly due to the GIL, which will help avoid some of the more serious threading issues.

    There's a list here: http://coreygoldberg.blogspot.com/2008/09/python-thread-synchronization-and.html that might be useful.

    Though atomic nature of those operation just means that you won't have an entirely inconsistent state if you have two threads accessing a dictionary at the same time. So you wouldn't have a corrupted value. However you would (as with most multi-threading programming) not be able to rely on the specific order of those atomic operations.

    So to cut a long story short...

    If you have fairly simple requirements and aren't to bothered about the ordering of what get written into the cache then you can use a dictionary and know that you'll always get a consistent/not-corrupted value (it just might be out of date).

    If you want to ensure that things are a bit more consistent with regard to reading and writing then you might want to look at Django's local memory cache:

    http://code.djangoproject.com/browser/django/trunk/django/core/cache/backends/locmem.py

    Which uses a read/write lock for locking.

提交回复
热议问题