跳转到主要内容

一个简单的管理命令,可以从大型会话表中删除过期的会话,而不会杀死网站。

项目描述

一个简单的管理命令,可以从大型会话表中删除过期的会话,而不会杀死网站。

主要用例是如果您有一个有数百万过时会话的实时应用程序。运行常规的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次。根据数据库负载(假设删除需要1秒),这将需要(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

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

django-graceful-session-cleanup-0.2.2.tar.gz (3.6 kB 查看哈希值)

上传时间

支持

AWSAWS 云计算和安全赞助商DatadogDatadog 监控FastlyFastly CDNGoogleGoogle 下载分析MicrosoftMicrosoft PSF赞助商PingdomPingdom 监控SentrySentry 错误日志StatusPageStatusPage 状态页面