uwsgi.cache_set() 不在 mule 进程内的单独线程中工作
uwsgi.cache_set() not working in separate thread inside a mule process
为了实验,我在 mule 进程中设置了缓存 uwsgi.cache_set('test', data)
。缓存已按预期设置。
现在我生成一个线程,我可以从中访问该缓存
线程已在 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()
问题是为什么会发生这种情况以及如何从线程内部设置缓存。
好吧,我好像用错了函数 (cache_set
) 而不是 cache_update
。
uwsgi.cache_set(key, value[, expire, cache_name])
Set a value in the cache. If the key is already set but not expired,
it doesn’t set anything.
uwsgi.cache_update(key, value[, expire, cache_name])
Update a value in the cache. This always sets the key, whether it was
already set before or not and whether it has expired or not.
为了实验,我在 mule 进程中设置了缓存 uwsgi.cache_set('test', data)
。缓存已按预期设置。
现在我生成一个线程,我可以从中访问该缓存
线程已在 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()
问题是为什么会发生这种情况以及如何从线程内部设置缓存。
好吧,我好像用错了函数 (cache_set
) 而不是 cache_update
。
uwsgi.cache_set(key, value[, expire, cache_name])
Set a value in the cache. If the key is already set but not expired, it doesn’t set anything.
uwsgi.cache_update(key, value[, expire, cache_name])
Update a value in the cache. This always sets the key, whether it was already set before or not and whether it has expired or not.