When using uwsgi to start a Python script or project, there will be a problem that global variables cannot be accessed.
Everyone knows that uwsgi can specify the number of child processes. I set the number of processes to 1, then I have two functions, so he will only handle one (at the same time). uwsgi runs multiple processes independently for thread concurrency, so he has multiple global variables.
-
The way uwsgi works is to start a specified process listening port, execute the corresponding uwsgi entry file when the request is received, and then return the result to the specified listening port.
-
When it comes to modifying global variables in a python or multiple threads accessing a variable, you will not be able to get the modification of the global variables.
Because each request will re-execute the python script again, so that each time will re-initialize global variables.
Solution
It is not advisable to place global variables in a process, so take them out and put them in a "public access area" to allow all processes to access and modify the global variables of the "public access area".
- At present, this "public access area" can be implemented in multiple ways:
-
Use ready-made databases: redis, mongodb, mysql, etc .;
-
Implement a management process to manage the data yourself (refer to multiprocessing module);
-
Take advantage of uwsgi's built-in cache function
Here that focused on ways to solve the third, you can maximize does not rely on third-party tools, and by means of uwsgi
a built-in method.
关于“新一代”的缓存,它由uWSGI 1.9引入。 对于旧式缓存 (现在简单称其为“web缓存”),可以看看 WebCaching框架
uWSGI包含了一个非常快速、全内存访问、零IPC、SMP安全、不断优化、高度可调的、键值存储的简单的“缓存框架”。
单个uWSGI实例可以使用不同的设置,出于不同的目的,创建无限个不同的“缓存”。
- Start by the following command
uwsgi
:
uwsgi --cache2 name=mycache,items=100 --socket :3031
This will create a ”mycache”
cache named , which has up to 100 items. Each item can be up to 64k.
- Or
uwsgi
add the following configuration profile (the first line is a comment description)
; create a cache with 100 items (default size per-item is 64k)
cache2 = name=mycache,items=100,blocksize=5000000
Configuration creates a named mycache
cache, it has up to 100 entries, each entry is the largest of: 5MB, which blocksize
is used to set the size of a single buffer, in bytes (Byte)
Add good uwsgi
can cache after python
setting the variable needs of public access to the script, by introducing the uwsgi
method calls the following module and can use uwsgi
the cache.
-
cache_get(key[,cache])
-
cache_set(key,value[,expires,cache])
-
cache_update(key,value[,expires,cache])
-
cache_exists(key[,cache])
-
cache_del (key [, cache])
-
cache_clear([cache])
If you call the cache API
of language / platform distinguishes between strings and bytes (for example Python 3和Java
), then you must assume that the key is a string, byte value (or under java, is an array of bytes). Otherwise, the keys and values are strings with no specific encoding, because internally, the cache values and cache keys are simple binary blobs.
-
expires
Parameters (default is 0, disables) the object is failed seconds (when not provided andpurge_lru
time, removed from the cache scavenger, see below) -
cache
The parameter is the so-called "magic identifier", and its syntax iscache[@node]
The specific configuration method can refer to:
In addition, it is recommended to use uwsgi_cache
this module, which utilizes python
its own pickle
module uwsgi
in cache api
a simple package that uwsgi
can be cached python
objects, is the need to use care that the cache pickle
object does not support such python
modules or open files, etc. are not here to explain the specific reasons, can refer to the python pickle
module this blog post.
uwsgi_cache
This module itself provides two methods, the specific content can refer to the uwsgi_cache
home page
- Use
uwsgi_cache
module complete cache: First configure the cache:
uwsgi test.py --cache2 name=api,items=1000
Then test.py
call the file uwsgi
cache api
globally cache
from uwsgi_cache.cache import CacheManager
# :params:
# name: the name of the cache that you would like to create
# expires: the expire time of the cache data
# 这里需要传入两个参数 ,name 为缓存的名字,
# expires 为缓存的有效期, 0 表示禁用(即该缓存永不失效)
cache = CacheManager("cache_name_01", 0)
# 调用缓存方法
cache = 0
if not cache.exists("count"):
cache.set("count",0)
print("count value is %d",0)
else:
count = cache.get("count")
count +=1
print("count value is %d",count)
cache.set("count",count)
def application(environ, start_response):
status = '200 OK'
response_body = "count value is %d",count
response_header = [('Content-Type', 'text/html')]
start_response(status, response_header)
return [response_body.encode()]
Start uwsgi, you will find that the value of count will continue to increase with each request call.
uwsgi test.py --cache2 name=api,items=1000
Reference original address: https://my.oschina.net/ghimi/blog/2705982