Python MySQLdb select 来自无限线程的新数据
Python MySQLdb select fresh data from a infinite thread
使用 python2.7 和 MySQLdb 我想要一个无限线程,它会不时地执行 SELECT 查询。似乎只有第一个 SELECT 被考虑在内,当我修改 table 中的数据以便或多或少地获得那些永远不会改变的结果时。
这是我的方法:
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__ 打开我的光标而不是关闭它没有帮助。有什么建议吗?
我在 cursor.fetchall()
之后添加 self.db.commit() 解决了我的问题
谢谢!
使用 python2.7 和 MySQLdb 我想要一个无限线程,它会不时地执行 SELECT 查询。似乎只有第一个 SELECT 被考虑在内,当我修改 table 中的数据以便或多或少地获得那些永远不会改变的结果时。
这是我的方法:
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__ 打开我的光标而不是关闭它没有帮助。有什么建议吗?
我在 cursor.fetchall()
之后添加 self.db.commit() 解决了我的问题谢谢!