python MySQLdb 问题 - 程序已的后台运行每90秒获取数据,却无法获取最新的值
2013-05-24 09:34
771 查看
这是我在stackoverflow.com 中发现的处理方法,通过 autocommit解决问题!
URL: http://stackoverflow.com/questions/5943418/chronic-stale-results-using-mysqldb-in-python
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
URL: http://stackoverflow.com/questions/5943418/chronic-stale-results-using-mysqldb-in-python
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Chronic stale results using MySQLdb in Python
up vote6down votefavorite3 | My Python program queries a set of tables in a MySQL DB, sleeps for 30 seconds, then queries them again, etc. The tables in question are continuously updated by a third-party, and (obviously) I would like to see the new results every 30 seconds. Let's say my query looks like this: "select * from A where A.key > %d"% maxValueOfKeyFromLastQueryRegularly I will see that my program stops finding new results after one or two iterations, even though new rows are present in the tables. I know new rows are present in the tables because I can see them when I issue the identical query from interactive mysql (i.e. not from Python). I found that the problem goes away in Python if I terminate my connection to the database after each query and then establish a new one for the next query. I thought maybe this could be a server-side caching issue as discussed here: Explicit disable MySQL query cache in some parts of program However: When I check the interactive mysql shell, it says that caching is on. (So if this is a caching problem, how come the interactive shell doesn't suffer from it?) If I explicitly execute SET SESSION query_cache_type = OFFfrom within my Python program, the problem still occurs. Creating a new DB connection for each query is the only way I've been able to make the problem go away. How can I get my queries from Python to see the new results that I know are there? pythonmysqlcaching
| ||||||||||||
|
3 Answers
activeoldestvotesup vote6down voteaccepted | This website and this website contain information on the same problem. In order to keep your tables up to date, you must commit your transactions. Use db.commit()to do this. As mentioned by the post below me, you can remove the need for this by enabling auto-commit. this can be done by running db.autocommit(True) Also, auto-commit is enabled in the interactive shell, so this explains why you didn't have the problem there.
| ||||||
|
up vote1down vote | You can enable auto-commit automatically in MySQLdb! Try the following:conn = MySQLdb.Connect("host","user","password")conn.autocommit(True)This gives you the same behavior that you are used to in the interactive shell.
| ||
up vote-1down vote | You may want to check the transaction isolation level of your database. The behavior you describe is what you may expect if it is set to REPEATABLE-READ. You may want to change it to READ-COMMITTED. Since the original poster of the problem mentions that he is merely querying the database, it cannot be a commit that was forgotten. Inserting a commit seems to be a workaround though since it causes a new transaction to begin; and a new snapshot may need to be established. Still, having to insert a commit before every select doesn't sound like good programming practices to me. There is no python code to show since the solution lies in correctly configuring the database. DO check MySQL documentation at http://dev.mysql.com/doc/refman/5.5/en/set-transaction.html. REPEATABLE READ This is the default isolation level for InnoDB. For consistent reads, there is an important difference from the READ COMMITTED isolation level: All consistent reads within the same transaction read the snapshot established by the first read. This convention means that if you issue several plain (nonlocking) SELECT statements within the same transaction, these SELECT statements are consistent also with respect to each other. See Section 14.3.9.2, “Consistent Nonlocking Reads”. READ COMMITTED A somewhat Oracle-like isolation level with respect to consistent (nonlocking) reads: Each consistent read, even within the same transaction, sets and reads its own fresh snapshot. See Section 14.3.9.2, “Consistent Nonlocking Reads”. Checking the configured isolation level: >mysql >SELECT@@GLOBAL.tx_isolation,@@tx_isolation;+-----------------------+-----------------+|@@GLOBAL.tx_isolation |@@tx_isolation |+-----------------------+-----------------+| REPEATABLE-READ | REPEATABLE-READ |+-----------------------+-----------------+1rowinset(0.01 sec)Setting the transaction isolation level to READ-COMMITTED mysql>SET GLOBAL tx_isolation='READ-COMMITTED';Query OK,0rows affected (0.00 sec)mysql>SET SESSION tx_isolation='READ-COMMITTED';Query OK,0rows affected (0.00 sec)mysql>SELECT@@GLOBAL.tx_isolation,@@tx_isolation;+-----------------------+----------------+|@@GLOBAL.tx_isolation |@@tx_isolation |+-----------------------+----------------+| READ-COMMITTED | READ-COMMITTED |+-----------------------+----------------+1rowinset(0.01 sec)mysql>And run the application again … |
This website and this website contain information on the same problem. In order to keep your tables up to date, you must commit your transactions. Use db.commit()to do this. As mentioned by the post below me, you can remove the need for this by enabling auto-commit. this can be done by running db.autocommit(True) Also, auto-commit is enabled in the interactive shell, so this explains why you didn't have the problem there. |
相关文章推荐
- 后台运行python程序输出缓冲区问题
- 后台运行python程序 遇到缓冲区问题
- 微信小程序:wx.request之post请求后端无法获取数据的问题
- linux服务器wget无法成功解析域名及程序获取外网数据不稳定问题
- jquery datatable 通过ajax从后台获取每个分页数据后,搜索框无法搜索问题
- 后台运行python程序 遇到缓冲区问题
- 后台运行python程序 遇到缓冲区问题
- 后台运行python程序 遇到缓冲区问题
- python获取程序运行目录和脚本目录
- hibernate无法获取最新数据
- Android 无法获取正在运行的程序
- SpringBind对象到页面时,用ajax提交form表单内容,后台无法获取绑定对象问题
- 升级ADT22 后程序无法运行问题解决方法
- vs 项目编译运行报错:无法启动此程序,因为计算机中丢失 osg130-osgDB.dll。尝试重新安装该程序以解决此问题
- 解决Sublime Text 2运行程序时用户无法输入的问题
- SQL Server 安装程序无法获取 ASPNET 帐户的系统帐户信息。若要继续,请重新安装 .NET Framework,然后再次运行 SQL 。
- 微信小程序开发之真机测试 地图定位 map API 无法获取当前位置的问题
- android基础(android程序的后台运行问题)
- 解决在ajax回调函数中循环遍历data数组无法获取数组下标为0之后的重复数据的问题
- Python程序无法在命令行下运行