【问题标题】:Python MySQLdb select fresh data from a infinite threadPython MySQLdb 从无限线程中选择新数据
【发布时间】:2017-01-04 16:39:59
【问题描述】:

使用 python2.7 和 MySQLdb,我希望有一个无限线程,可以不时执行 SELECT 查询。似乎只有第一个 SELECT 被考虑在内,当我修改表中的数据以便获得或多或少的结果时,这些结果永远不会改变。

这是我的方法:

def getUrlsToCrawl(self):
    dateNowUtc = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S")
    cursor = self.db.cursor()
    cursor.execute('SELECT id, url, nbErrors FROM mytable WHERE nbErrors < %s AND domain = %s and nextCrawl < %s', (self.MAX_RETRY, self.domain, dateNowUtc))
    print cursor._last_executed
    urls = cursor.fetchall()
    print urls
    cursor.close()
    return urls

在 __init__ 处建立与数据库的连接,并在我终止线程时关闭。

这是日志

SELECT id, url, nbErrors FROM mytable WHERE nbErrors < 10 AND domain = 'foo.com' and nextCrawl < '2017-01-04T16:33:52'
((6L, u'http://www.foo.com/foo.php', 4L),)

SELECT id, url, nbErrors FROM mytable WHERE nbErrors < 10 AND domain = 'foo.com' and nextCrawl < '2017-01-04T16:33:59'
((6L, u'http://www.foo.com/foo.php', 4L),)

SELECT id, url, nbErrors FROM mytable WHERE nbErrors < 10 AND domain = 'foo.com' and nextCrawl < '2017-01-04T16:34:06'
((6L, u''http://www.foo.com/foo.php'', 4L),)

SELECT id, url, nbErrors FROM mytable WHERE nbErrors < 10 AND domain = 'foo.com' and nextCrawl < '2017-01-04T16:34:13'
((6L, u''http://www.foo.com/foo.php'', 4L),)

如果我将此条目的 nbErrors 从 4 更改为 12,则结果保持不变,但查询不应得到此结果。

我试图在 __init__ 处打开我的光标而不是关闭它并没有帮助。有什么建议吗?

【问题讨论】:

  • 打印时显示什么?
  • 当我打印光标时? &lt;MySQLdb.cursors.Cursor object at 0x7f32deef1950&gt;每次调用内存都在变化
  • 不,我的意思是:print urls
  • 它已经在主要问题中了。我认为这可能是缓存问题,我尝试了SELECT SQL_NO_CACHE,但行为相同
  • 我认为问题出在您的查询上

标签: python multithreading mysql-python


【解决方案1】:

我在 cursor.fetchall() 之后添加 self.db.commit() 解决了我的问题

谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多