国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python寫的一個(gè)定時(shí)重跑獲取數(shù)據(jù)庫數(shù)據(jù)

2019-11-25 16:26:07
字體:
供稿:網(wǎng)友

做大數(shù)據(jù)的童鞋經(jīng)常會(huì)寫定時(shí)任務(wù)跑數(shù)據(jù),由于任務(wù)之間的依賴(一般都是下游依賴上游的數(shù)據(jù)產(chǎn)出),所以經(jīng)常會(huì)導(dǎo)致數(shù)據(jù)獲取失敗,因?yàn)楹芏嗳税l(fā)現(xiàn)數(shù)據(jù)失敗后

都會(huì)去查看日志,然后手動(dòng)去執(zhí)行自己的任務(wù)。下面我實(shí)現(xiàn)了一個(gè)自動(dòng)重復(fù)執(zhí)行去數(shù)據(jù)庫取數(shù),如果失敗后自動(dòng)重新去獲取,直到把數(shù)據(jù)獲取到。

建數(shù)據(jù)表:

CREATE TABLE `testtable` ( 2 `id` int(11) unsigned NOT NULL AUTO_INCREMENT, 3 `name` varchar(20) NOT NULL, 4 PRIMARY KEY (`id`) 5 ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

開始的時(shí)候數(shù)據(jù)表是空的,等到腳本重試第3秒的時(shí)候像數(shù)據(jù)庫插入數(shù)據(jù)。

以下是python代碼的實(shí)現(xiàn)

#!/usr/bin/env python #-*- coning:utf-8 -*- 3 4 import MySQLdb 5 from time import sleep 6 7 class GetData(object): 8 def __init__(self): 9 self.conn = '' 10 self.host = '127.0.0.1' 11 self.port = 3306 12 self.user = 'root' 13 self.passwd = '123456' 14 self.db = 'test' 15 self.cnum = 5 #set retry number 16 17 def init_connect(self): 18 self.conn = MySQLdb.connect(host=self.host, user=self.user, passwd=self.passwd, db=self.db, port=self.port, 19 charset='utf8') 20 21 def get_data(self): 22 self.init_connect 23 cur = self.conn.cursor 24 sql = "select * from testtable" 25 cur.execute(sql) 26 rs = cur.fetchall 27 cur.close 28 self.conn.close 29 return rs 30 31 def run(self): 32 count = 1 33 while (count <= self.cnum): 34 rs = self.get_data 35 if len(rs) > 0: 36 print len(rs) 37 break 38 39 print count 40 sleep(10) 41 count += 1 42 43 if __name__ == '__main__': 44 gd = GetData 45 gd.run

自己可以手動(dòng)執(zhí)行,在代碼執(zhí)行到第3秒的時(shí)候,執(zhí)行下面的sql

insert into testtable(`name`) values ('123'),('456'),('789'),('1111'),('3222'),('444');

下面是定時(shí)的任務(wù)的腳本

00 08 * * * cd /home/python/lsh_sync; python getdata.py >> getdata.log 2>&1

以上所述是小編給大家介紹的Python寫的一個(gè)定時(shí)重跑獲取數(shù)據(jù)庫數(shù)據(jù),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平陆县| 莲花县| 思茅市| 旺苍县| 哈密市| 余庆县| 宁陕县| 辉南县| 崇信县| 丰台区| 宁津县| 六盘水市| 塘沽区| 略阳县| 观塘区| 海兴县| 肃宁县| 广水市| 苏州市| 自贡市| 华安县| 绵阳市| 馆陶县| 长子县| 阳高县| 曲沃县| 霍州市| 泾川县| 共和县| 临夏市| 桃园县| 工布江达县| 崇左市| 大冶市| 治县。| 合川市| 东山县| 舞阳县| 民权县| 卢湾区| 图片|