全栈工程师开发手册 (作者:栾鹏)
架构系列文章


安装torndb包

pip install torndb

python3.6修改

修改C:\Program Files\Python36\lib\site-packages\torndb.py或者/usr/local/lib/python3.6/dist-packages/torndb.py

#!/usr/bin/env python
#
# Copyright 2009 Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License."""A lightweight wrapper around MySQLdb.Originally part of the Tornado framework.  The tornado.database module
is slated for removal in Tornado 3.0, and it is now available separately
as torndb.
"""from __future__ import absolute_import, division, with_statementimport copy
import itertools
import logging
import os
import time
import pymysql
print(os.path.split(os.path.realpath(__file__))[0])
try:# import MySQLdb.constants# import MySQLdb.converters# import MySQLdb.cursorsimport pymysql.constantsimport pymysql.convertersimport pymysql.cursors
except ImportError:# If MySQLdb isn't available this module won't actually be useable,# but we want it to at least be importable on readthedocs.org,# which has limitations on third-party modules.if 'READTHEDOCS' in os.environ:pymysql = Noneelse:raiseversion = "0.3"
version_info = (0, 3, 0, 0)class Connection(object):"""A lightweight wrapper around MySQLdb DB-API connections.The main value we provide is wrapping rows in a dict/object so thatcolumns can be accessed by name. Typical usage::db = torndb.Connection("localhost", "mydatabase")for article in db.query("SELECT * FROM articles"):print article.titleCursors are hidden by the implementation, but other than that, the methodsare very similar to the DB-API.We explicitly set the timezone to UTC and assume the character encoding toUTF-8 (can be changed) on all connections to avoid time zone and encoding errors.The sql_mode parameter is set by default to "traditional", which "gives an error instead of a warning"(http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html). However, it can be set toany other mode including blank (None) thereby explicitly clearing the SQL mode."""def __init__(self, host, database, user=None, password=None,max_idle_time=7 * 3600, connect_timeout=10,  # 设置连接超时时间,时间是秒time_zone="+0:00", charset = "utf8", sql_mode="TRADITIONAL"):self.host = hostself.database = databaseself.max_idle_time = float(max_idle_time)args = dict(conv=CONVERSIONS, use_unicode=True, charset=charset,db=database, init_command=('SET time_zone = "%s"' % time_zone),connect_timeout=connect_timeout, sql_mode=sql_mode)if user is not None:args["user"] = userif password is not None:args["passwd"] = password# We accept a path to a MySQL socket file or a host(:port) stringif "/" in host:args["unix_socket"] = hostelse:self.socket = Nonepair = host.split(":")if len(pair) == 2:args["host"] = pair[0]args["port"] = int(pair[1])else:args["host"] = hostargs["port"] = 3306self._db = Noneself._db_args = argsself._last_use_time = time.time()try:self.reconnect()except Exception:logging.error("Cannot connect to MySQL on %s", self.host,exc_info=True)def __del__(self):self.close()def close(self):"""Closes this database connection."""if getattr(self, "_db", None) is not None:self._db.close()self._db = Nonedef reconnect(self):"""Closes the existing database connection and re-opens it."""self.close()self._db = pymysql.connect(**self._db_args)self._db.autocommit(True)def iter(self, query, *parameters, **kwparameters):"""Returns an iterator for the given query and parameters."""self._ensure_connected()cursor = pymysql.cursors.SSCursor(self._db)try:self._execute(cursor, query, parameters, kwparameters)column_names = [d[0] for d in cursor.description]for row in cursor:yield Row(zip(column_names, row))finally:cursor.close()def query(self, query, *parameters, **kwparameters):"""Returns a row list for the given query and parameters."""cursor = self._cursor()try:self._execute(cursor, query, parameters, kwparameters)column_names = [d[0] for d in cursor.description]return [Row(itertools.zip_longest(column_names, row)) for row in cursor]finally:cursor.close()def get(self, query, *parameters, **kwparameters):"""Returns the (singular) row returned by the given query.If the query has no results, returns None.  If it hasmore than one result, raises an exception."""rows = self.query(query, *parameters, **kwparameters)if not rows:return Noneelif len(rows) > 1:raise Exception("Multiple rows returned for Database.get() query")else:return rows[0]# rowcount is a more reasonable default return value than lastrowid,# but for historical compatibility execute() must return lastrowid.def execute(self, query, *parameters, **kwparameters):"""Executes the given query, returning the lastrowid from the query."""return self.execute_lastrowid(query, *parameters, **kwparameters)def execute_lastrowid(self, query, *parameters, **kwparameters):"""Executes the given query, returning the lastrowid from the query."""cursor = self._cursor()try:self._execute(cursor, query, parameters, kwparameters)return cursor.lastrowidfinally:cursor.close()def execute_rowcount(self, query, *parameters, **kwparameters):"""Executes the given query, returning the rowcount from the query."""cursor = self._cursor()try:self._execute(cursor, query, parameters, kwparameters)return cursor.rowcountfinally:cursor.close()def executemany(self, query, parameters):"""Executes the given query against all the given param sequences.We return the lastrowid from the query."""return self.executemany_lastrowid(query, parameters)def executemany_lastrowid(self, query, parameters):"""Executes the given query against all the given param sequences.We return the lastrowid from the query."""cursor = self._cursor()try:cursor.executemany(query, parameters)return cursor.lastrowidfinally:cursor.close()def executemany_rowcount(self, query, parameters):"""Executes the given query against all the given param sequences.We return the rowcount from the query."""cursor = self._cursor()try:cursor.executemany(query, parameters)return cursor.rowcountfinally:cursor.close()update = execute_rowcountupdatemany = executemany_rowcountinsert = execute_lastrowidinsertmany = executemany_lastrowiddef _ensure_connected(self):# Mysql by default closes client connections that are idle for# 8 hours, but the client library does not report this fact until# you try to perform a query and it fails.  Protect against this# case by preemptively closing and reopening the connection# if it has been idle for too long (7 hours by default).if (self._db is None or(time.time() - self._last_use_time > self.max_idle_time)):self.reconnect()self._last_use_time = time.time()def _cursor(self):self._ensure_connected()return self._db.cursor()def _execute(self, cursor, query, parameters, kwparameters):try:return cursor.execute(query, kwparameters or parameters)except OperationalError:logging.error("Error connecting to MySQL on %s", self.host)self.close()raiseclass Row(dict):"""A dict that allows for object-like property access syntax."""def __getattr__(self, name):try:return self[name]except KeyError:raise AttributeError(name)if pymysql is not None:# Fix the access conversions to properly recognize unicode/binaryFIELD_TYPE = pymysql.constants.FIELD_TYPEFLAG = pymysql.constants.FLAGCONVERSIONS = copy.copy(pymysql.converters.conversions)field_types = [FIELD_TYPE.BLOB, FIELD_TYPE.STRING, FIELD_TYPE.VAR_STRING]if 'VARCHAR' in vars(FIELD_TYPE):field_types.append(FIELD_TYPE.VARCHAR)for field_type in field_types:CONVERSIONS[field_type] = [(FLAG.BINARY, str)].append(CONVERSIONS[field_type])# Alias some common MySQL exceptionsIntegrityError = pymysql.IntegrityErrorOperationalError = pymysql.OperationalError

自定类


# 如果安装torndb,记得要替换/usr/local/lib/python3.6/dist-packages文件夹下的torndb.py文件import queue,threading,torndb# mysql异步线程池
class MysqlConnPool(object):def __init__(self, host, database, user, pwd, max_conns=30):self.idle_conn = queue.Queue()self.pool_size = 0self.max_conns = max_connsself.conn_params = (host, database, user, pwd)self.poll_size_mutex = threading.Lock()def _get_conn_from_pool(self):if self.idle_conn.empty() and self.pool_size < self.max_conns:conn = torndb.Connection(*self.conn_params, time_zone="+8:00")self.poll_size_mutex.acquire()self.pool_size += 1self.poll_size_mutex.release()return connreturn self.idle_conn.get()# 查询函数def query(self, sqlcommand,*args, **kwargs):conn = self._get_conn_from_pool()res = conn.query(sqlcommand,*args, **kwargs)self.idle_conn.put(conn)return res# 执行+查询函数def execute(self,sqlcommand, *args, **kwargs):conn = self._get_conn_from_pool()res = conn.execute(sqlcommand,*args, **kwargs)self.idle_conn.put(conn)return res# 提交函数def commit(self):passif __name__=="__main__":import time,random,pymysqlmysqlpool=MysqlConnPool('192.168.xx.xx:32001','database_name','root','introcks')result = mysqlpool.query('select id from capture order by id desc limit 1')print(result)feature_arr = bytearray([random.randint(1,244) for x in range(0,2068)])result = mysqlpool.execute('insert into capture (feature,image_id,device_id,create_time) values(%s,%s,%s,%s)',*(feature_arr,'12312312312', '1000', int(time.time())))print(result)

tornado使用mysql 连接池(torndb)相关推荐

  1. mysql多个字符串连接池_使用Coroutine\Channel实现一个简单的MySQL连接池

    Channel通道,类似于go语言的chan,支持多生产者协程和多消费者协程,Swoole底层自动实现了协程的切换和调度 Channel实现原理 通道与PHP的Array类似,仅占用内存,没有其他额外 ...

  2. mysql内连接和外连接的区别_Swoole4创建Mysql连接池

    一 .什么是mysql连接池 场景:每秒同时有1000个并发,但是这个mysql同时只能处理400个连接,mysql会宕机. 解决方案:连接池,这个连接池建立了200个和mysql的连接,这1000个 ...

  3. MySQL会回收使用中的连接吗_Node.js实现mysql连接池使用事务自动回收连接的方法示例...

    本文实例讲述了Node.js实现mysql连接池使用事务自动回收连接的方法.分享给大家供大家参考,具体如下: var mysql = require('mysql'), Connection = re ...

  4. mysql清理连接数缓存,MySQL连接池、线程缓存、线程池的区别

    1. MySQL连接池 连接池通常实现在client端,是指应用(客户端)预先创建一定的连接,利用这些连接服务于客户端所有的DB请求.如果某一个时刻,空闲的连接数小于DB的请求数,则需要将请求排队,等 ...

  5. php 协程 mysql_实现一个协程版mysql连接池

    实现一个协程版的mysql连接池,该连接池支持自动创建最小连接数,自动检测mysql健康:基于swoole的chanel. 最近事情忙,心态也有点不积极.技术倒是没有落下,只是越来越不想写博客了.想到 ...

  6. node mysql limit_node中mysql连接池的connectionLimit指什么,它和mysql的最小连接数和最大连接数的关系是什么?...

    问题1:node中mysql连接池的connectionLimit指什么,它和mysql的最小连接数和最大连接数的关系是什么 问题2:mysql max_connections是什么,max_used ...

  7. swoole原生mysql进程池_swoole的mysql连接池怎么弄

    swoole的mysql连接池怎么弄 发布时间:2020-12-28 09:54:07 来源:亿速云 阅读:68 作者:小新 这篇文章给大家分享的是有关swoole的mysql连接池怎么弄的内容.小编 ...

  8. node mysql 连接池创建_Node.js使用MySQL连接池的方法实例

    本文实例讲述了Node.js使用MysqL连接池的方法.分享给大家供大家参考,具体如下: Nodejs如何使用MysqL Nodejs要连接MysqL,可以使用Nodejs的MysqL驱动来实现.比如 ...

  9. 关于Tomcat与MySQL连接池问题的详解

    转载自   关于Tomcat与MySQL连接池问题的详解 研究了一天,终于有所收获,希望对大家有所帮助.首先请大家注意:这里尤其讨论Tomcat5.5版本中遇到的问题,为什么尤其单对这个版本,我一会儿 ...

最新文章

  1. Android旋转切换条目,Android:当我添加动态新的ListView条目时,ListView中的旋转器会丢失它们的值...
  2. intellij导入scala工程不识别scala语言
  3. 【pyqt5学习】——给窗口添加图标
  4. jsp的include两种使用方法
  5. 解决在工具栏Chrome图标上点击右键会显示“常去网站”和“最后关闭网站”的问题
  6. php---需要判断远程URL是否有效
  7. php字符串去重和去空,php去空格
  8. 阿里云SSL证书免费申请和部署方法((DigiCert 免费版 SSL-图文教程)
  9. 【大厂面试】面试官看了赞不绝口的Redis笔记(三)分布式篇
  10. 家庭数据中心-私有云服务器定义和选择
  11. 论文编辑与投稿——论文页眉、页码编辑,以及换章时偶数页设置成空白页的操作
  12. win10禁用数字签名(win10系统禁用数字签名)
  13. excel冻结窗口怎么设置_excel打印区域怎么设置?excel表格打印区域怎么设置?
  14. MySQL行转列函数
  15. 软考知识点梳理--国家信息化体系六要素
  16. html accordion折叠菜单动态,jQuery EasyUI 布局插件 – Accordion 折叠面板 | 菜鸟教程...
  17. 【军师联盟】--鬼才(军师祭酒)郭嘉郭奉孝(如何成为领导最信任的人)
  18. 【转】“蚁族” 的生活方式画像
  19. SIGCOMM 2020 Topic Preview: Video + Machine Learning
  20. 云平台计算机配置,云电脑平台获《APEX》玩家好评!从此无需再为电脑配置而发愁!...

热门文章

  1. Android实现语音识别代码
  2. 怎么用matlab显示噪声,怎么用MATLAB产生噪声调频信号
  3. Vue-的基本使用和指令
  4. 【模拟】蓝桥20:蛇形填数
  5. Media Player Classic - HC 源代码分析 2:核心类 (CMainFrame)(1)
  6. c语言喂狗的作用,兽医忠告:用这几样食物喂狗,简直就是喂“砒霜”!
  7. java面向对象怎么学_Java面向对象入门
  8. android开发根据分辨率设置高度,【android】根据屏幕分辨率设置底栏高度
  9. File转换成MultiPartFile
  10. PLSQL中文显示乱码