Created
March 12, 2019 15:19
-
-
Save fanliugen/68ed6f55ecf95df42f35a5c9763aacf7 to your computer and use it in GitHub Desktop.
Asynchronous SqlServer Operations with Twisted
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| # @Author : LoganFan | |
| # @Time : 2019-02-16 | |
| # @File : run.py | |
| from SqlServerDB import SqlServerDB | |
| from PBParser import PBParser | |
| import os | |
| import pyodbc | |
| from twisted.internet import reactor | |
| import logging | |
| from logging.handlers import RotatingFileHandler | |
| logger = logging.getLogger('') | |
| logger.setLevel(logging.DEBUG) | |
| Rthandlder = RotatingFileHandler('WISCANConclusionUpdate_log.txt',maxBytes=5*1024*1024) | |
| formatter = logging.Formatter('%(asctime)s - - %(message)s') | |
| Rthandlder.setFormatter(formatter) | |
| logger.addHandler(Rthandlder) | |
| class Manager(object): | |
| def __init__(self): | |
| DRIVERs = pyodbc.drivers() | |
| DRIVER = DRIVERs[0] if DRIVERs else '' | |
| self.config = {} | |
| dirPath =os.path.dirname(os.path.abspath(__file__)) | |
| txtPath = os.path.join(dirPath,'config.txt') | |
| with open(txtPath, 'r', encoding='utf-8') as f: | |
| for line in f: | |
| if line.strip() and ('=' in line): | |
| v = [x.strip() for x in line.split('=')] | |
| self.config[v[0]] = v[1] | |
| self.dbManager = SqlServerDB(DRIVER=DRIVER, UID=self.config['DBUSER'], PWD=self.config['DBPASSWORD'], SERVER=self.config['DBHOST'], DATABASE=self.config['DBNAME']) | |
| self.pbParser = PBParser() | |
| def updateConclusionWithPB(self, imaPICNo): | |
| selectSql = "SELECT a.CHECKUNITID FROM IDR_WISCAN_CONCLUSION AS a INNER JOIN IDR_CHECK_UNIT AS b \ | |
| ON a.CHECKUNITID = b.ID INNER JOIN IDR_IMAGE AS c ON c.ID = b.IMAGEID AND c.PICNO='{}'".format(imaPICNo) | |
| self.dbManager.dbpool.runQuery(selectSql).addCallback(self.execUpdateDB,imaPICNo) | |
| reactor.run() | |
| def execUpdateDB(self,result,imaPICNo): | |
| serialNo = imaPICNo[-4:] | |
| day = imaPICNo[-8: -4] | |
| year = imaPICNo[-12: -8] | |
| equipNo = imaPICNo[-len(imaPICNo):-12] | |
| BaseDir = r'{}'.format(self.config['BaseDir']) | |
| dirPath = os.path.join(BaseDir, equipNo, year, day, serialNo) | |
| pb_path = '' | |
| # pb_path='/Users/logan/Desktop/深圳陆运/Source_data_20181224/0069/86755MB04201502060069_0.pb' | |
| logger.debug('pbPath :{}'.format(pb_path)) | |
| for dirpath, dirnames, filenames in os.walk(dirPath): | |
| for file_ in filenames: | |
| if '.pb' in file_: | |
| pb_path = os.path.join(dirpath, file_) | |
| break | |
| if pb_path: | |
| result_dict = self.pbParser.get_PbDict(pb_path) | |
| contraband = self.pbParser.get_contraband_result(result_dict) | |
| hiding = self.pbParser.get_hiding_result(result_dict) | |
| goodname = self.pbParser.get_goodname_result(result_dict) | |
| empty = self.pbParser.get_emptycontainer_result(result_dict) | |
| waste = self.pbParser.get_waste_result(result_dict) | |
| chaging = self.pbParser.get_changing_result(result_dict) | |
| coordinate = self.pbParser.get_cheti_cordinate(result_dict) | |
| time = self.pbParser.get_algorithm_time(result_dict) | |
| checkunitid = result[0][0] | |
| logger.debug('checkunitid : {}'.format(checkunitid)) | |
| updateSql = "UPDATE IDR_WISCAN_CONCLUSION SET WASTE='{waste}',ALGORITHM_TIME='{time}',CHANGING='{chaging}', \ | |
| EMPTY_CONTAINER='{empty}',TRUCK_COORDINATE='{coordinate}',RESERVE2='{goodname}',RESERVE3='{hiding}', \ | |
| RESERVE5='{contraband}' WHERE CHECKUNITID='{checkunitid}'".format(waste=waste, time=time, chaging=chaging, | |
| empty=empty, \ | |
| coordinate=coordinate, goodname=goodname, | |
| hiding=hiding, contraband=contraband, | |
| checkunitid=checkunitid) | |
| self.dbManager.dbpool.runOperation(updateSql).addCallback(self.receiveResult) | |
| def receiveResult(self,result): | |
| logging.debug("Update success!") | |
| reactor.stop() | |
| def errorquery(self, result): | |
| logging.debug("Error received!", result) | |
| reactor.stop() | |
| def main(imaPICNo): | |
| logging.debug('imaPICNO : {}'.format(imaPICNo)) | |
| manager = Manager() | |
| manager.updateConclusionWithPB(imaPICNo=imaPICNo) | |
| if __name__ == '__main__': | |
| imaPICNo = '86532FS03201808170037' | |
| checkunitid = 'A73E5FB3-5290-4876-9BCF-A9C610B723BE' | |
| main(imaPICNo) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| # @Author : LoganFan | |
| # @Time : 2019-02-16 | |
| # @File : SqlServerDB.py | |
| import pyodbc | |
| import sys | |
| from twisted.enterprise import adbapi | |
| import logging | |
| class SqlServerDB(object): | |
| def __init__(self, DRIVER, SERVER, DATABASE, UID, PWD): | |
| self.DRIVER = DRIVER | |
| self.SERVER = SERVER | |
| self.DATABASE = DATABASE | |
| self.UID = UID | |
| self.PWD = PWD | |
| self.dbpool = self.GetConnect() | |
| def GetConnect(self): | |
| if not self.DATABASE: | |
| raise (NameError,'no setting db info') | |
| try: | |
| dbpool = adbapi.ConnectionPool(dbapiName='pyodbc',DRIVER=self.DRIVER, SERVER = self.SERVER, DATABASE=self.DATABASE, UID=self.UID, PWD=self.PWD,charset="UTF-8") | |
| except Exception as ex: | |
| logging.error('SQL Server connecting error,reason is:{}'.format(str(ex))) | |
| sys.exit() | |
| return dbpool | |
| if __name__ == '__main__': | |
| config = {} | |
| with open('config.txt', 'r', encoding='utf-8') as f: | |
| for line in f: | |
| if line.strip() and ('=' in line): | |
| v = [x.strip() for x in line.split('=')] | |
| config[v[0]] = v[1] | |
| DRIVERs = pyodbc.drivers() | |
| DRIVER = DRIVERs[0] if DRIVERs else '' | |
| ms = SqlServerDB(DRIVER=DRIVER, UID=config['DBUSER'], PWD=config['DBPASSWORD'], SERVER=config['DBHOST'], DATABASE=config['DBNAME']) | |
| ms.GetConnect() | |
| print(ms) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment