首页 > 解决方案 > uwsgi.cache_set() 不在 mule 进程内的单独线程中工作

问题描述

uwsgi.cache_set('test', data)为了实验,我在 mule 进程中设置了一个缓存。缓存按预期设置。

现在我生成一个线程,我可以从中访问该缓存


线程在以下位置启用uwsgi.ini

[uwsgi]
threads = 4

mule.py

#Threaded function
def a_function():
    uwsgi.cache_set('test', b'NOT OK') <- Nothing happens here
    cache_return = uwsgi.cache_get('test') <- Returns b'OK' which means the cache did not overwrite the previous value.

if __name__ == '__main__':
    cache = uwsgi.cache_set('test', b'OK')  <- Works here
    cache_return = uwsgi.cache_get('test') <- Return b'OK', as expected
    t = Thread(target=a_function)
    t.start()

问题是为什么会发生这种情况以及如何从线程内部设置缓存。

标签: python-3.xmultithreadingcachinguwsgi

解决方案


好的,好像我使用了错误的函数 ( cache_set) 而不是cache_update.

uwsgi.cache_set(key, value[, expire, cache_name])

在缓存中设置一个值。如果密钥已设置但未过期,则不会设置任何内容


uwsgi.cache_update(key, value[, expire, cache_name])

更新缓存中的值。这总是设置 key,无论它之前是否已经设置,以及它是否已过期。


推荐阅读