使用MySQLdb时,如果创建一个连接connection,多个线程同时使用,会不会有问题?

在下文中,我们将模拟这种场景,看是否会出现问题。

1.示例

1.1 正常的情况

创建一个连接,两个线程同时使用这个连接,生成游标cursor,进行查询,并输出结果。

程序启动后,让线程1睡眠1s,保证让线程2线执行。

import MySQLdb
import threading
import time def get_connection():
host = "127.0.0.1"
port = 3306
user = "root"
passwd = "Aa123456" conn = MySQLdb.connect(host=host, port=port, user=user,passwd=passwd, connect_timeout=2, charset="utf8")
return conn def thread1_runtime(conn, sql):
time.sleep(1)
cursor = conn.cursor()
cursor.execute(sql) ret = cursor.fetchone()
thread_name = threading.current_thread().name
print("thread name:%s, ret:%s" %(thread_name, ret)) def thread2_runtime(conn, sql):
cursor = conn.cursor()
cursor.execute(sql) ret = cursor.fetchone()
thread_name = threading.current_thread().name
print("thread name:%s, ret:%s" %(thread_name, ret)) if __name__ == "__main__": thread1_sql = "select 1"
thread2_sql = "select 2" conn = get_connection() thread1 = threading.Thread(target=thread1_runtime, name="thread 001", args=(conn, thread1_sql))
thread2 = threading.Thread(target=thread2_runtime, name="thread 002", args=(conn, thread2_sql)) thread1.start()
thread2.start() thread1.join()
thread2.join() print("...main exit....")

从结果可以看到,一切正常。

output:

thread name:thread 002, ret:(2,)

thread name:thread 001, ret:(1,)

...main exit....

1.2 异常的情况

以下例子中,程序启动后,线程1和线程2使用同一连接创建新的游标cursor,执行查询。

接着,线程1睡眠1秒,线程2睡眠2秒。

最后各自打印查询结果。

import MySQLdb
import threading
import time def get_connection():
host = "127.0.0.1"
port = 3306
user = "root"
passwd = "Aa123456" conn = MySQLdb.connect(host=host, port=port, user=user,passwd=passwd, connect_timeout=2, charset="utf8")
return conn def thread1_runtime(conn, sql):
cursor = conn.cursor()
cursor.execute(sql) time.sleep(1)
ret = cursor.fetchone()
thread_name = threading.current_thread().name
print("thread name:%s, ret:%s" %(thread_name, ret)) def thread2_runtime(conn, sql):
cursor = conn.cursor()
cursor.execute(sql) time.sleep(2)
ret = cursor.fetchone()
thread_name = threading.current_thread().name
print("thread name:%s, ret:%s" %(thread_name, ret)) if __name__ == "__main__": thread1_sql = "select 1"
thread2_sql = "select 2" conn = get_connection() thread1 = threading.Thread(target=thread1_runtime, name="thread 001", args=(conn, thread1_sql))
thread2 = threading.Thread(target=thread2_runtime, name="thread 002", args=(conn, thread2_sql)) thread1.start()
thread2.start() thread1.join()
thread2.join() print("...main exit....")

output:

Exception in thread thread 002:

Traceback (most recent call last):

File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 916, in _bootstrap_inner

self.run()

File "/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 864, in run

self._target(*self._args, **self._kwargs)

File "one_conn_mutiple_cursor.py", line 28, in thread2_runtime

cursor.execute(sql)

File "/Users/lanyang/workspace/orange-service/.venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 198, in execute

res = self._query(query)

File "/Users/lanyang/workspace/orange-service/.venv/lib/python3.6/site-packages/MySQLdb/cursors.py", line 304, in _query

db.query(q)

File "/Users/lanyang/workspace/orange-service/.venv/lib/python3.6/site-packages/MySQLdb/connections.py", line 217, in query

_mysql.connection.query(self, query)

MySQLdb._exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now")

thread name:thread 001, ret:(1,)

...main exit....

从打印结果可以看到,线程1可以正常打印结果,但线程2报错了。

线程1睡眠先结束,去获取查询结果,可以打印。

线程2睡眠后结束,再去获取查询结果,已经无法获取结果了。

2.总结

从上面例子中,我们可以得出结论:

一个连接connection同一个时间点只能有一个cursor,执行sql,并获取结果。

所以,不要在多个线程中同时使用一个连接connection,否则会出现不可预料的结果。

python MySQLdb 一个连接connection多个cursor的更多相关文章

  1. Python MySQLdb模块连接操作mysql数据库实例_python

    mysql是一个优秀的开源数据库,它现在的应用非常的广泛,因此很有必要简单的介绍一下用python操作mysql数据库的方法.python操作数据库需要安装一个第三方的模块,在http://mysql ...

  2. python使用MySQLdb模块连接MySQL

    1.安装驱动 目前有两个MySQL的驱动,我们可以选择其中一个进行安装: MySQL-python:是封装了MySQL C驱动的Python驱动:mysql-connector-python:是MyS ...

  3. python MySQLdb用法,python中cursor操作数据库(转)

    数据库连接 连接数据库前,请先确认以下事项: 您已经创建了数据库 TESTDB. 在TESTDB数据库中您已经创建了表 EMPLOYEE EMPLOYEE表字段为 FIRST_NAME, LAST_N ...

  4. 【Python】如何基于Python写一个TCP反向连接后门

    首发安全客 如何基于Python写一个TCP反向连接后门 https://www.anquanke.com/post/id/92401 0x0 介绍 在Linux系统做未授权测试,我们须准备一个安全的 ...

  5. python MySQLdb连接mysql失败(转载)

    最近了解了一下django,数据库选用了mysql, 在连接数据库的过程中,遇到一点小问题,在这里记录一下,希望能够对遇到同样的问题的朋友有所帮助,少走一些弯路.关于django,想在这里也额外说一句 ...

  6. 一个jdbc connection连接对应一个事务

    Spring保证在methodB方法中所有的调用都获得到一个相同的连接.在调用methodB时,没有一个存在的事务,所以获得一个新的连接,开启了一个新的事务. Spring保证在methodB方法中所 ...

  7. Python MySQLdb 模块使用方法

    import MySQLdb 2.和数据库建立连接 conn=MySQLdb.connect(host="localhost",user="root",pass ...

  8. python mysqldb 教程

    MySQL Python 教程 (1)下面是在Python中使用MySql数据库的教程,涵盖了Python对MySql的基础操作,主要采用了MySQLdb模块,下面的代码都是基于Ubuntu Linu ...

  9. Python MySQLdb 学习总结(转)

    转自http://www.cnblogs.com/coser/archive/2012/01/12/2320741.html 感谢@糖拌咸鱼 任何应用都离不开数据,所以在学习python的时候,当然也 ...

随机推荐

  1. dubbo学习笔记三(全注解)

    完全用注解替换掉之前的部分配置文件 项目结构 下面给出服务的的部分代码 [DubboConfiguration] @Configuration @EnableDubbo(scanBasePackage ...

  2. mybatis查询返回的对象不为null,但是属性值为null

    返回的对象不为null,但是属性值为null 代码如下: <resultMap id="BaseResultMap" type="com.trhui.ebook.d ...

  3. python中同步、多线程、异步IO、多线程对IO密集型的影响

    目录 1.常见并发类型 2.同步版本 3.多线程 4.异步IO 5.多进程 6.总结 1.常见并发类型 I/ O密集型: 蓝色框表示程序执行工作的时间,红色框表示等待I/O操作完成的时间.此图没有按比 ...

  4. Redis常见的七种使用场景

    简单字符串缓存实战简单队列实战简单发布订阅实战简单计数器实战排行榜实战简单字符串悲观锁实战简单事务的乐观锁实战   简单字符串缓存实战 $redis->connect('127.0.0.1', ...

  5. 织梦网站dedecms防止挂马的思路

    DedeCms做为国内使用最为广泛使用人数最多的CMS之一,经常爆出漏洞,每个漏洞的爆出,影响都是一大片,轻则被人挂广告.弹框,重则服务器成为肉机,宝贵数据丢失.那么有什么办法可以提高DedeCms的 ...

  6. 微信小程序 canIUse

    wx.canIUse(); 微信文档中定义在API中,可以理解为一个函数. 返回值: true 或者 false 示例: // 在JS文件的函数中进行使用 console.log(wx.canIUse ...

  7. 集合(四) Hashtable

    2.Hashtable Hashtable,顾名思义,哈希表,本来是已经被淘汰的内容,但在某一版本的Java将其实现了Map接口,因此也成为常用的集合类,但是hashtable由于和hashmap十分 ...

  8. public、protected、private

    public(公有):可以在任何地方被访问 protected(受保护):可以被其自身以及其子类和父类访问,类的对象也不可以访问 private(私有):只能被其定义所在的类访问,类的对象也不可以访问 ...

  9. Python 3标准库 第十一章 网路通信

    网路通信11.1 ipaddress  : Internet 地址 ipaddress模块提供了处理IPv4和IPv6 11.1.2  网络 CMDB  11.2  socket:网络通信 11.2. ...

  10. vuex中mapState、mapMutations、mapAction的理解

    当一个组件需要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余.为了解决这个问题,我们可以使用 mapState 辅助函数帮助我们生成计算属性. // 在单独构建的版本中辅助函数为 Vue ...