一个简单的管理命令,可以从大的会话表中删除过期的会话而不杀死站点。
django-graceful-session-cleanup的Python项目详细描述
一个简单的管理命令,可以从大型会话表中删除过期的会话,而不会终止站点。
主要的用例是如果您有一个实时应用程序,在数据库中有数百万个陈旧的会话。运行 常规的python manage.py cleanup可能会锁定您的数据库数小时,实际上会杀死您的站点。
使用django-graceful-session-cleanup可以逐渐删除所有会话。
为了实现这一点,它总是只删除每个查询的少量过期会话,然后休眠 几秒钟。有命令行选项来控制要删除的会话的间隔、睡眠时间和数量 每次间隔。
安装
安装软件包:
pip install django-graceful-session-cleanup
添加到INSTALLED_APPS:
INSTALLED_APPS = [ .... 'graceful_session_cleanup', ]
运行管理命令:
python manage.py graceful_session_cleanup
您可以使用以下几个选项:
python manage.py graceful_session_cleanup --sleep-seconds 9 --delete-count 1000 --iteration-count 200
这将删除1000过期的会话条目,等待5秒,以便其他进程可以使用 数据库,然后重复这个200次。取决于数据库负载(假设删除需要1s) 将占用(9s + 1s) * 200 = 2000s。
命令行上有帮助:
$ python manage.py help graceful_session_cleanup Usage: django graceful_session_cleanup [options] Can be run as a cronjob or directly to clean out old data from the database (only expired sessions at the moment). Does this in a live db friendly way by never hogging the connection too long. Options: -v VERBOSITY, --verbosity=VERBOSITY Verbosity level; 0=minimal output, 1=normal output, 2=all output --settings=SETTINGS The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided, the DJANGO_SETTINGS_MODULE environment variable will be used. --pythonpath=PYTHONPATH A directory to add to the Python path, e.g. "/home/djangoprojects/myproject". --traceback Print traceback on exception -s SLEEP_SECONDS, --sleep-seconds=SLEEP_SECONDS How long to sleep between each delete operation. -c DELETE_COUNT, --delete-count=DELETE_COUNT How many expired sessions to delete per iteration. -i ITERATION_COUNT, --iteration-count=ITERATION_COUNT How many iterations to run. --version show program's version number and exit -h, --help show this help message and exit