Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
本文介绍了FlASK+PyMySQL给出错误,没有属性';setTimeout';的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
几天以来,我在我的Flask站网站上时不时地出现此错误
[Sat Sep 14 00:04:31.016107 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] File "/var/www/xxxx/__init__.py", line 1612, in showArticleTitlePage
[Sat Sep 14 00:04:31.016119 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] language = session['locale'])
[Sat Sep 14 00:04:31.016130 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] File "/var/www/cardsrealm/SQL.py", line 1376, in get_articles_before
[Sat Sep 14 00:04:31.016146 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] cursor.execute(query, t)
[Sat Sep 14 00:04:31.016176 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] File "/var/www/xxxx/venv/lib/python3.6/site-packages/pymysql/cursors.py", line 170, in execute
[Sat Sep 14 00:04:31.016188 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] result = self._query(query)
[Sat Sep 14 00:04:31.016198 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] File "/var/www/xxxx/venv/lib/python3.6/site-packages/pymysql/cursors.py", line 328, in _query
[Sat Sep 14 00:04:31.016209 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] conn.query(q)
[Sat Sep 14 00:04:31.016219 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] File "/var/www/xxxx/venv/lib/python3.6/site-packages/pymysql/connections.py", line 517, in query
[Sat Sep 14 00:04:31.016230 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] self._affected_rows = self._read_query_result(unbuffered=unbuffered)
[Sat Sep 14 00:04:31.016241 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] File "/var/www/xxxx/venv/lib/python3.6/site-packages/pymysql/connections.py", line 732, in _read_query_result
[Sat Sep 14 00:04:31.016251 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] result.read()
[Sat Sep 14 00:04:31.016262 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] File "/var/www/xxxx/venv/lib/python3.6/site-packages/pymysql/connections.py", line 1075, in read
[Sat Sep 14 00:04:31.016273 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] first_packet = self.connection._read_packet()
[Sat Sep 14 00:04:31.016297 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] File "/var/www/xxxx/venv/lib/python3.6/site-packages/pymysql/connections.py", line 674, in _read_packet
[Sat Sep 14 00:04:31.016310 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] recv_data = self._read_bytes(bytes_to_read)
[Sat Sep 14 00:04:31.016321 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] File "/var/www/xxxx/venv/lib/python3.6/site-packages/pymysql/connections.py", line 688, in _read_bytes
[Sat Sep 14 00:04:31.016332 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] self._sock.settimeout(self._read_timeout)
[Sat Sep 14 00:04:31.016342 2019] [wsgi:error] [pid 4693:tid 140670017648384] [remote 66.249.66.44:60013] AttributeError: 'NoneType' object has no attribute 'settimeout'
此setTimeout时有发生。我注意到有人说我应该连接并关闭连接以使其成为单线程安全。
self.conn = self.create_connection()
cursor = self.conn.cursor()
query = ''' SELECT MY QUERY '''.format(self.xxxColumn, self.xxxColumn)
cursor.execute(query, t)
result = cursor.fetchall()
cursor.close()
self.close_connection()
上面显示了我是如何处理它的。我在我的SQL中添加了以下内容:
connect_timeout = 200
read_timeout = 200
write_timeout = 200
max_allowed_packet = 1073741824
但是我仍然有这个错误!
推荐答案
我遇到了与您相同的错误。
我在代码中使用mysqlpool
DB_MIN_CACHED = 10
DB_MAX_CACHED = 10
DB_MAX_SHARED = 20
DB_MAX_CONNECYIONS = 150
DB_BLOCKING = True
DB_MAX_USAGE = 0
DB_SET_SESSION = None
DB_CREATOR = pymysql
import dbutils
from dbutils.pooled_db import PooledDB
import pymysql, os, configparser
from pymysql.cursors import DictCursor
class BasePymysqlPool(object):
def __init__(self, envconfig):
self.DB_HOST = envconfig.DB_HOST
self.DB_PORT = envconfig.DB_PORT
self.DB_DATABASE = envconfig.DB_DATABASE
self.DB_USER = envconfig.DB_USER
self.DB_PASSWORD = envconfig.DB_PASSWORD
self.CHARSET = envconfig.CHARSET
self.DB_MIN_CACHED = envconfig.DB_MIN_CACHED
self.DB_MAX_CACHED = envconfig.DB_MAX_CACHED
self.DB_MAX_SHARED = envconfig.DB_MAX_SHARED
self.DB_MAX_CONNECYIONS = envconfig.DB_MAX_CONNECYIONS
self.DB_BLOCKING = envconfig.DB_BLOCKING
self.DB_MAX_USAGE = envconfig.DB_MAX_USAGE
self.DB_SET_SESSION = envconfig.DB_SET_SESSION
self.DB_CREATOR = envconfig.DB_CREATOR
class MyPymysqlPool(BasePymysqlPool):
__pool = None
def __init__(self, conf_env=None):
self.conf = conf_env
super(MyPymysqlPool, self).__init__(self.conf)
self._conn = self.__getConn()
self._cursor = self._conn.cursor()
def __getConn(self):
if MyPymysqlPool.__pool is None:
__pool = PooledDB(creator=self.DB_CREATOR,
mincached=self.DB_MIN_CACHED,
maxcached=self.DB_MAX_CACHED,
host=self.DB_HOST,
port=self.DB_PORT,
user=self.DB_USER,
passwd=self.DB_PASSWORD,
db=self.DB_DATABASE,
use_unicode=False,
charset=self.CHARSET,
cursorclass=DictCursor)
return __pool.connection()
def getconn(self):
self._conn = self.__getConn()
self._cursor = self._conn.cursor()
def getOne(self, sql, param=None):
if param is None:
count = self._cursor.execute(sql)
else:
count = self._cursor.execute(sql, param)
if count > 0:
result = self._cursor.fetchone()
else:
result = False
return result
def __query(self, sql, param=None):
if param is None:
count = self._cursor.execute(sql)
else:
count = self._cursor.execute(sql, param)
return count
def update(self, sql, param=None):
return self.__query(sql, param)
def dispose(self, isEnd=1):
if isEnd == 1:
self.end('commit')
else:
self.end('rollback')
self._cursor.close()
self._conn.close()
def end(self, option='commit'):
if option == 'commit':
self._conn.commit()
else:
self._conn.rollback()
mysqlhelper= MyPymysqlPool(config_db)
sql = '''select md5_id from queryresult
where businessid = %s and md5_id = %s;
'''
mysqlhelper.getconn()
result_db_num = mysqlhelper.update(sql, [businessid, md5_id])
mysqlhelper.end()
mysqlhelper.dispose()
我尝试将DB_MAX_CONNECYIONS=100
设置为DB_MAX_CONNECYIONS=150
,但似乎不起作用。
我注意到,要求越少,错误就越少。 所以,我用锁来解决这个问题。
import threading
lock = threading.Lock()
lock.acquire()
curos.excure(command,data)
lock.release()
在我的代码中:
lock.acquire()
mysqlhelper.getconn()
result_db_num = mysqlhelper.update(sql, [businessid, md5_id])
mysqlhelper.end()
mysqlhelper.dispose()
lock.release()
那么,完成!
这篇关于FlASK+PyMySQL给出错误,没有属性';setTimeout';的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:FlASK+PyMySQL给出错误,没有属性';setTimeout';
基础教程推荐
猜你喜欢
- 与不使用派生表的查询相比,使用派生表的查询 2021-01-01
- 哪个是 Rails 应用程序的最佳数据库? 2022-01-01
- 为 SQL Server 设置 Maven 依赖项 2021-01-01
- my.cnf 文件在 macOS 上的位置 2021-01-01
- (+) 在 Oracle SQL 中有什么作用? 2021-01-01
- 替换存储在 SQL Server 数据库列中的 XML 中的节点名 2021-01-01
- 如何将参数传递给使用 sqlcmd 调用的 SQL Server 脚本? 2021-01-01
- SSIS:在数据流中使用 System::TaskName 2021-01-01
- Sql*plus 总是返回退出代码 0? 2021-01-01
- 如何在没有密码提示的情况下执行 mysqldump? 2021-01-01