mysql连续查询_mysql持续查询

持续读取mysql:

#!/usr/bin/env python

#coding=utf8

import os

import sys

import time

import Queue

import MySQLdb

from threading import Thread

from termcolor import cprint

queues = Queue.Queue(maxsize=100)

dblen = 0

thread_num = 100

#从输入点开始读取,

def Readmysql(dbl):

conn= MySQLdb.connect(

host='192.168.1.33',

port = 3306,

user='root',

passwd='root',

db = 'userdata',

use_unicode=True,

charset="utf8"

)

cur = conn.cursor()

d = 20

onedblen = 0

sql = 'select count(id) from user'

cur.execute(sql)

conn.commit()

currentlen = cur.fetchone() #获取所有的数据量

#读取大于输入点的数据

while currentlen > dbl:

sql = 'select id FROM user LIMIT %s,%s' %(dbl,d)#每次读取20条

cur.execute(sql)

conn.commit()

dbl += d #递增

data = cur.fetchall()

if len(data) == d: #刚好读到20条

for i in data:

queues.put(i)

elif len(data) < d: #未读到20条,表明已读完

for i in data:

queues.put(i)

onedblen = dbl - d + len(data)#数据总量

cprint('[+] DB has '+str(onedblen)+' data!','red')

break

else:

cprint('[-] xxx','yellow')

time.sleep(10)

conn.close()

#监控mysql数据变化,如果超过输入点point调用readmysql

def Monitormysql(point):

conn= MySQLdb.connect(

host='192.168.1.33',

port = 3306,

user='root',

passwd='root',

db = 'userdata',

use_unicode=True,

charset="utf8"

)

cur = conn.cursor()

global dblen

dblen = point

while True:

sql = 'select count(id) from user'

cur.execute(sql)

conn.commit()

data = cur.fetchone()

if data[0] > dblen:

cprint('[+] DB has new data!','blue')

readdb = threading.Thread(target = Readmysql,args=(dblen,))

readdb.start()

readdb.join()

dblen = data[0]

else:

print 'No new data...'

time.sleep(5)

time.sleep(8)

conn.close()

#处理数据

class HandleThread(threading.Thread):

def __init__(self):

threading.Thread.__init__(self)

def run(self):

tid = queues.get()

oid = tid[0].encode('unicode-escape')

print oid

def Handledata():

while True:

if queues.empty():

time.sleep(1)

cprint('[-] No data from queues','yellow')

else:

for i in range(thread_num):

sth = HandleThread()

sth.start()

sth.join()

if __name__ == '__main__':

# you may input a start point:

if len(sys.argv) == 1:

print "Usage:[%s] + db starting point!!" % sys.argv[0]

sys.exit()

try:

startpoint = int(sys.argv[1])

cprint('[+] Begin '+sys.argv[1]+' line..','red')

monitodb = threading.Thread(target = Monitormysql,args=(startpoint,))

hd = threading.Thread(target = Handledata)

monitodb.start()

time.sleep(5)

hd.start()

except Exception,e:

raise Exception('Exception %s' % str(e))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值