当前位置: 首页 > news >正文

直销软件网站开发在线葡京在线葡京

直销软件网站开发,在线葡京在线葡京,重庆招生官网,怎样用dw做 网站首页python 项目中使用 celery 中导致mysql数据库连接耗尽记录【mysql数据库连接池使用错误】 结论:由于使用 celery 进行项目的多任务管理,在worker任务定义的过程中,使用了 dbutils 中的 PooledDB 连接池进行 mysql数据库连接, 因此…

python 项目中使用 celery 中导致mysql数据库连接耗尽记录【mysql数据库连接池使用错误】

结论:由于使用 celery 进行项目的多任务管理,在worker任务定义的过程中,使用了 dbutils 中的 PooledDB 连接池进行 mysql数据库连接, 因此系统在并发执行 worker 任务时, 将产生大量的数据库连接,最终导致mysql数据库连接耗尽

我在该 celery 项目中,使用了 20 个 worker 节点,每个 worker 节点开启了 32 个prefork子进程, 每个子进程中将产生连接池,而 PooledDB 连接池最大为 200个, 最终该项目产生的数据库连接数为 20 * 32 * 200 =12800 个数据库连接 ;

而我用的阿里云 rds 数据库, 8核16G 该配置连接数为 1600 个,所以每次只要大量并发执行有数据库操作的任务, 就会导致数据库连接耗尽, 所有连接该数据库的服务都挂了
在这里插入图片描述

下面是我在项目中使用的数据库连接的部分代码

from dbutils.pooled_db import PooledDBtry:_pool: PooledDB = PooledDB(creator=pymysql, mincached=0, maxcached=10, blocking=True,maxconnections=200,  # 连接池允许的最大连接数,0和None表示不限制连接数maxshared=100,  # 允许的最大共享连接数(默认值 0 或 None 表示所有连接都是专用的)maxusage=10,host=conf["host"], port=conf["port"], user=conf["user"], passwd=conf["pwd"],db=conf["dbname"], use_unicode=True, charset='utf8mb4',cursorclass=SSDictCursor,setsession=['SET AUTOCOMMIT = 1'])
except Exception as e:raise e

由于项目中的 celery 默认使用 prefork 多进程的模式; 但是 PooledDB 在 prefork 类型的多进程模式下不生效

celery 并发模式文档摘要

Overview of Concurrency Options

  • prefork: The default option, ideal for CPU-bound tasks and most use cases. It is robust and recommended unless there’s a specific need for another model.
  • eventlet and gevent: Designed for IO-bound tasks, these models use greenlets for high concurrency. Note that certain features, like soft_timeout, are not available in these modes. These have detailed documentation pages linked below.
  • solo: Executes tasks sequentially in the main thread.
  • threads: Utilizes threading for concurrency, available if the concurrent.futures module is present.
  • custom: Enables specifying a custom worker pool implementation through environment variables.

部分翻译:

  • celery 默认选项,非常适合 CPU 密集型任务和大多数用例。它非常可靠,除非对其他模型有特定需求,否则建议使用它。

dbutils 中的 PooledDB 文档摘要

Notes

If you are using one of the popular object-relational mappers SQLObject or SQLAlchemy, you won’t need DBUtils, since they come with their own connection pools. SQLObject 2 (SQL-API) is actually borrowing some code from DBUtils to split the pooling out into a separate layer.

Also note that when you are using a solution like the Apache webserver with mod_python or mod_wsgi, then your Python code will be usually run in the context of the webserver’s child processes. So if you are using the pooled_db module, and several of these child processes are running, you will have as much database connection pools. If these processes are running many threads, this may still be a reasonable approach, but if these processes don’t spawn more than one worker thread, as in the case of Apache’s “prefork” multi-processing module, this approach does not make sense. If you’re running such a configuration, you should resort to a middleware for connection pooling that supports multi-processing, such as pgpool or pgbouncer for the PostgreSQL database.

部分翻译:

  • 当您使用诸如带有 mod_python 或 mod_wsgi 的 Apache Web 服务器之类的解决方案时,您的 Python 代码通常将在 Web 服务器的子进程的上下文中运行。因此,如果您使用 pooled_db 模块,并且其中几个子进程正在运行,您将拥有尽可能多的数据库连接池。如果这些进程正在运行许多线程,这可能仍然是一种合理的方法,但如果这些进程不产生多个工作线程,就像 Apache 的“prefork”多处理模块的情况一样,这种方法就没有意义。
参考文档:
  • dbutils 官方文档

  • celery 官方文档

http://www.hkea.cn/news/8205/

相关文章:

  • 用什么手机软件做网站汕头网站建设方案优化
  • 自适应网站一般做几个尺寸品牌推广方案思维导图
  • 自己做电影资源网站情感链接
  • 遵义市网站建设长沙网站seo排名
  • 批量查询网站是否正常长春网站建设策划方案
  • 做网站南京百度网盘下载速度慢破解方法
  • 路北网站制作seo优化推荐
  • 装修公司企业网站开发规划百度官方营销推广平台
  • 睢宁建网站百度搜索资源平台提交
  • 微信怎么做小程序的电脑系统优化软件十大排名
  • 网站建设的要求有哪些广告推广赚钱
  • 网站建设价格在哪济南兴田德润优惠吗深圳百度竞价推广
  • 河南企业网官方网站河南推广网站的公司
  • 做网站的编程语言免费网站建设模板
  • 做网站友汇网网站优化建议
  • 企业网站备案域名可以用个人的核心关键词和长尾关键词举例
  • 上海做网页公司莆田seo推广公司
  • 少儿编程自学网站企业网站建设规划
  • 厦门网站建设培训学校网站制作基本流程
  • 中国扬州 政府门户网站建设方案怎么做网络销售
  • 现在的网站前端用什么做青岛关键词优化seo
  • 公司网站海报怎么做计算机基础培训机构
  • 什么行业 网站seo网络推广到底是做什么的
  • 东城网站建设百度网盘官网网页版
  • 兰溪市网站建设公司上海广告公司
  • 淘宝单页面网站semir
  • 广州动态网站开发百度百家官网入口
  • 网站设计部的优化google官网下载安装
  • 海口今天发现一例抖音seo公司
  • 鞍山网站seo的实现方式