Python writes a timed re-run get database data

Source: Internet
Author: User
This article to you to share based on Python write a timed re-run to obtain database data method, very good, with reference value, the need for reference of friends

Big data of children's shoes often write timed task run data, due to the dependencies between tasks (generally downstream dependent on upstream data output), so often lead to data acquisition failure, because many people find that data failure

Will go to check the logs and then manually perform their own tasks. Below I implemented an automatic repeat to go to the database fetch, if failed to automatically regain access, until the data obtained.

Build Data Sheet:

CREATE TABLE ' testtable ' (2 ' id ' int (one) unsigned NOT NULL auto_increment, 3 ' name ' varchar () is not NULL, 4 PRIMARY KEY ( ' ID ') 5) Engine=innodb auto_increment=1 DEFAULT Charset=utf8;

At the beginning, the data table is empty, wait until the script retries the first 3 seconds to insert data like a database.

The following is the implementation of Python code

#!/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 = ' Self.host = ' 127.0.0.1 ' self.port = 3306 = ' root ' Self.user = ' 123 456 ' self.db = ' Test ' self.cnum = 5 #set retry number, Def init_connect (self): Self.conn = MySQLdb.connect (h Ost=self.host, User=self.user, passwd=self.passwd, Db=self.db, Port=self.port, charset= ' UTF8 ') def get_data ( Self): self.init_connect cur = self.conn.cursor sql = "SELECT * from TestTable" Cur.execute (sql) rs = cur.f Etchall Cur.close Self.conn.close return Rs. def run (self): Count = 1 (Count <= self.cnum): 3  4 rs = Self.get_data If Len (rs) > 0:36 print len (RS) Notoginseng break All-in-print Count + sleep (Ten) Count + = 1 __name__ = = ' __main__ ': GD = GetData Gd.run

You can do this manually, and when the code executes to the first 3 seconds, execute the following SQL

Insert into TestTable (' name ') VALUES (' 123 '), (' 456 '), (' 789 '), (' 1111 '), (' 3222 '), (' 444 ');

The following is a script for timed tasks

XX * * * CD/HOME/PYTHON/LSH_SYNC; Python getdata.py >> getdata.log 2>&1

The above is a small series to introduce you to the Python write a timed re-run to get database data, I hope that we have some help, if you have any questions please give me a message, small series will promptly reply to everyone. Thank you very much for your support for topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.