python threadsafe object cache

前端 未结 6 949
感动是毒
感动是毒 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:14

    For a thread safe object you want threading.local:

    from threading import local
    
    safe = local()
    
    safe.cache = {}
    

    You can then put and retrieve objects in safe.cache with thread safety.

提交回复
热议问题