Python Threading多线程简单例子
import sys
import pymysql
import os
from prometheus_client import Gauge,start_http_server
import time
import threading
#v_host=os.popen('echo $HOSTNAME')
#hostname=v_host.read()
#hstname="".join(hostname)
#print(hostname.strip())
class MySQL_Status_Output:
def __init__(self,host,port,user,password):
try:
self.db = pymysql.connect(host=host,port=port,user=user,password=password)
except Exception as e:
print('Database Connection Error')
print(e)
def mysql_select_colum_sql(self,sql):
try:
self.cursor = self.db.cursor(cursor = pymysql.cursors.DictCursor)
self.cursor.execute(sql)
v_result=self.cursor.fetchall()
return v_result
except Exception as e:
print('SQL Result One Column Error')
print(e)
def mysql_select_sql(self,sql):
try:
self.cursor = self.db.cursor()
self.cursor.execute(sql)
col=self.cursor.description
v_result=self.cursor.fetchall()
return v_result,col
except Exception as e:
print('SQL Result More Column Error')
print(e)
def close_db(self):
self.db.close()
while True:
time.sleep(t)
try:
pro_db = MySQL_Status_Output('127.0.0.1',3306,'dbadmin','dbadmin')
ccpay_sql = pro_db.mysql_select_colum_sql(sql_exec)
pro_db.close_db()
if int(len(ccpay_sql))==1:
for i in range(len(ccpay_sql)):
c=ccpay_sql[i]
a=list(c.keys())
for j in range(len(a)):
print(a[j],':',c.get(a[j]))
ccpayGauge.labels(mylabelname=a[j]).set(c.get(a[j]))
else:
pass
except Exception as e:
print('One Colum ccpayGauge Error')
print(e)
while True:
time.sleep(t)
try:
pro_db = MySQL_Status_Output('127.0.0.1',3306,'dbadmin','dbadmin')
ccpay_sql,col_name = pro_db.mysql_select_sql(sql_exec)
if int(len(ccpay_sql))>0:
bs_name=[]
for i in range(len(ccpay_sql)):
bs_name.append(ccpay_sql[i][0])
print(bs_name)
for bsname in range(len(bs_name)):
col_name_list=[]
for i in range(len(col_name)):
col_name_list.append(col_name[i][0])
col_name_tuple=tuple(col_name_list)
ccpay_value_list=[]
for j in range(len(ccpay_sql)):
if ccpay_sql[j][0]==bs_name[bsname]:
ccpay_value_list=ccpay_sql[j]
me=dict(zip(col_name_tuple,ccpay_value_list))
for key in me:
bn=str(bs_name[bsname]+key)
bkey=str(me[key])
if str(bs_name[bsname])==str(bkey):
pass
else:
print(bn,':',bkey)
ccpayGauge.labels(mylabelname=bn).set(bkey)
except Exception as e:
print('More Colums ccpayGauge Error')
print(e)
def ccpay_sql_table_exec(sql_exec,t):
while True:
time.sleep(t)
try:
pro_db = MySQL_Status_Output('127.0.0.1',3306,'dbadmin','dbadmin')
ccpay_sql = pro_db.mysql_select_colum_sql(sql_exec)
pro_db.close_db()
resultdc=eval(str(ccpay_sql).replace('Decimal', '').replace('(', '').replace(')', ''))
for x in resultdc:
print(x)
ccpayTable.labels(A_MERCHANT=x['MERCHANT'],B_MACHINE_STATUS=x['MACHINE_STATUS']).set(x['QUANTITY'])
except Exception as e:
print('Table ccpayTable Error')
print(e)
def ccpay_sql_table_sub(sql_exec,t):
while True:
time.sleep(t)
try:
pro_db = MySQL_Status_Output('127.0.0.1',3306,'dbadmin','dbadmin')
ccpay_sql = pro_db.mysql_select_colum_sql(sql_exec)
pro_db.close_db()
resultdc=eval(str(ccpay_sql).replace('Decimal', '').replace('(', '').replace(')', ''))
for x in resultdc:
print(x)
ccpayTablesub.labels(B_ACCOUNT=x['账号'],A_MERCHANT=x['商户号'],C_SUCCESS=str(x['成功笔数']), D_CONCAT=str(x['成功率'])).set(x['成功金额'])
except Exception as e:
print('Table ccpayTablesub Error')
print(e)
start_http_server(9900)
ccpayGauge = Gauge('ccpayGauge','Description of gauge', ['mylabelname'])
ccpayTable = Gauge('ccpayTable','Description of table',['A_MERCHANT','B_MACHINE_STATUS'])
ccpayTablesub = Gauge('ccpayTablesub','Description of tablesub', ['B_ACCOUNT', 'A_MERCHANT', 'C_SUCCESS', 'D_CONCAT'])
try:
##全盘交易统计
Total_Transaction_Statistics = threading.Thread(target=ccpay_sql_one_exec,args=(" select count(1) '全盘交易笔数',count(case when trade_status !='FAILED' then 1 else null end) '全盘出码成功',count(case when trade_status ='SUCCESS' then 1 else null end) '全盘交易成功',count(case when notify_status ='SUCCESS' then 1 else null end) '全盘通知成功',sum(case when trade_status ='SUCCESS' then TRADE_AMOUNT else null end)/100 '全盘成功金额',count(case when trade_status ='FAILED' and trade_errmsg = '指定设备无响应' then 1 else null end) '指定设备无响应',count(case when trade_status ='FAILED' and trade_errmsg = '找不到可用的设备' then 1 else null end) '找不到可用的设备',TRUNCATE(count(case when trade_status ='SUCCESS' then 1 else null end)*100/count(1),2) '全盘交易成功率' from ifpay_ccpay.trade_info where CREATE_TIME > curdate() ",60,))
##可用设备数
Available_Machine_Quantity = threading.Thread(target=ccpay_sql_one_exec,args=(" select count(1) '可用设备' from ifpay_ccpay.machine where ENABLE_STATUS = '1' ",60,))
##商户交易统计
Merchant_Transaction_Statistics = threading.Thread(target=ccpay_sql_more_exec,args=(" select MERCHANT_name '商户名称',case when total is null then 0 else total end '交易笔数',case when qrcode is null then 0 else qrcode end '出码成功',case when success is null then 0 else success end '交易成功',case when notify is null then 0 else notify end '通知成功',case when amount is null then 0 else amount end '成功金额' from (select a.MERCHANT_name MERCHANT_name,count(1) total,count(case when trade_status !='FAILED' then 1 else null end) qrcode,count(case when trade_status ='SUCCESS' then 1 else null end) success,count(case when notify_status ='SUCCESS' then 1 else null end) notify,sum(case when trade_status ='SUCCESS' then TRADE_AMOUNT else null end)/100 amount from ifpay_ccpay.trade_info,ifpay_ccpay.merchant a where MERCHANT_NO=a.MERCHANT_id and CREATE_TIME > curdate() group by a.MERCHANT_name) a ",60,))
##全天支付成功率
Total_Payment_Success_Rate = threading.Thread(target=ccpay_sql_more_exec,args=(" select method '支付方式',TRUNCATE(count(case when trade_status ='SUCCESS' then 1 else null end)*100/count(*),2) '-支付成功率' from ifpay_ccpay.trade_info where CREATE_TIME > curdate() group by method ",60,))
##30分钟内支付成功率
Total_Payment_Success_Rate_30Min = threading.Thread(target=ccpay_sql_more_exec,args=("select method '支付方式',(case when rait is null then 0 else rait end) '-30分钟内支付成功率' from (select method ,TRUNCATE(count(case when trade_status ='SUCCESS' then 1 else null end)*100/count(case when trade_status !='FAILED' then 1 else null end),2) rait from ifpay_ccpay.trade_info where CREATE_TIME between date_add(now(), interval - 30 minute) and now() group by method) a",300,))
##30分钟内,商户支付成功率
Merchant_Payment_Success_Rate_30Min = threading.Thread(target=ccpay_sql_more_exec,args=("select MERCHANT,(case when rait is null then 0 else rait end) '-商户支付成功率' from (select concat(b.MERCHANT_ID,'-',a.method) MERCHANT,TRUNCATE(count(case when a.trade_status ='SUCCESS' then 1 else null end)*100/count(case when a.trade_status !='FAILED' then 1 else null end),2) rait from ifpay_ccpay.trade_info a,ifpay_ccpay.merchant b where a.MERCHANT_NO=b.MERCHANT_ID and a.CREATE_TIME between date_add(now(), interval - 30 minute) and now() group by concat(b.MERCHANT_ID,a.method)) a",300,))
##机器故障/可用情况
Total_Machine_State = threading.Thread(target=ccpay_sql_table_exec,args=(" select MERCHANT_id 'MERCHANT',case when (ENABLE_STATUS=0) then '不可用' when (ENABLE_STATUS=1) then '可用' else 'null' end 'MACHINE_STATUS', count(*) 'QUANTITY' from ifpay_ccpay.machine group by MERCHANT_ID,case when (ENABLE_STATUS=0) then '不可用' when (ENABLE_STATUS=1) then '可用' else 'null' end order by 1 ",60,))
##子商户支付详情
Sub_Merchant_Payment = threading.Thread(target=ccpay_sql_table_sub,args=(" select payee_account_no '账号', merchant_no '商户号', trade_success_count '成功笔数', amount '成功金额',CONCAT(ROUND(trade_success_count*100/trade_count),'%') '成功率' from (select payee_account_no, merchant_no, sum(case trade_status when 'SUCCESS' then 1 else 0 end) trade_success_count, count(*) trade_count, sum(trade_amount)/100 amount from ifpay_ccpay.trade_info where create_time>current_date() group by payee_account_no, merchant_no order by sum(trade_amount) desc) c where trade_success_count>0 limit 10 ",60,))
Total_Transaction_Statistics.start()
Available_Machine_Quantity.start()
Merchant_Transaction_Statistics.start()
Total_Payment_Success_Rate.start()
Total_Payment_Success_Rate_30Min.start()
Merchant_Payment_Success_Rate_30Min.start()
Total_Machine_State.start()
Sub_Merchant_Payment.start()
except Exception as e:
print('Threading Is Wrong')
print(e)
Python Threading多线程简单例子的更多相关文章
- python多线程简单例子
python多线程简单例子 作者:vpoet mail:vpoet_sir@163.com import thread def childthread(threadid): print "I ...
- C#多线程简单例子讲解
C#多线程简单例子讲解 标签: 多线程c#threadobjectcallbacktimer 分类: C#(7) 转载网址:http://www.knowsky.com/540518.html .NE ...
- java 多线程简单例子
实现线程的方式是一,继承Thread类,重写父类的run()方法 二,实现接口Runnable中的run()方法. 下面是简单的例子 例子1:银行存取钱问题 package com.direct.de ...
- Python threading(多线程)
threading模块在较低级别thread模块之上构建更高级别的线程接口. 一.threading模块定义了以下函数和对象: threading.active_count() 等同于threadin ...
- QT多线程简单例子
在Qt中实现多线程,除了使用全局变量.还可以使用信号/槽机制. 以下例子使用信号/槽机制. 功能: 在主线程A界面上点击按钮,然后对应开起一个线程B.线程B往线程A发送一个字符串,线程A打印出来. 1 ...
- python网络编程——简单例子
客户端(client.py) import socket import sys port = 70 host = sys.argv[1] filename = sys.argv[2] s = sock ...
- python threading多线程
import threading import time def print_time(threadName, delay, iterations): start = int(time.time()) ...
- pyqt5多线程-简单例子
一.主要代码逻辑 from PyQt5 import QtWidgets, QtCore from testqt.TEST_QT_FROM import Ui_Dialog import sys fr ...
- Python初学——多线程Threading
接着上篇继续跟着沫凡小哥学Python啦 1.1 什么是多线程 Threading 多线程可简单理解为同时执行多个任务. 多进程和多线程都可以执行多个任务,线程是进程的一部分.线程的特点是线程之间可以 ...
随机推荐
- mongodb 有一个坑 报错 no mongos proxies found in seed list
mongoose 的报当我从 mongoose@4.5.2 升级到 mongoose@4.6.5的时候,出现了一个问题: Unhandled rejection MongoError: no mong ...
- ACM-ICPC 2018 沈阳赛区网络预赛 Made In Heaven(K短路)题解
思路:K短路裸题 代码: #include<queue> #include<cstring> #include<set> #include<map> # ...
- 精巧好用的DelayQueue 转
我们谈一下实际的场景吧.我们在开发中,有如下场景 a) 关闭空闲连接.服务器中,有很多客户端的连接,空闲一段时间之后需要关闭之.b) 缓存.缓存中的对象,超过了空闲时间,需要从缓存中移出.c) 任务超 ...
- 基于nodejs环境,用npm简单搭建一个本地服务器Live-server的使用
用npm 或者cnpm进行全局安装 cnpm install -g live-server 运行后就可以直接给你虚拟一个本地服务器,而且还可以热同步 运行 live-server
- redis持久化RDB和AOF-转载
Redis 持久化: 提供了多种不同级别的持久化方式:一种是RDB,另一种是AOF. RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot). AO ...
- NetMagic Simple Overview
参考: NetMagic Startup: How to develop NetMagic rapidly NetMagic Simple Overview NetMagic 是什么? NetMagi ...
- 【TCP/IP详解 卷一:协议】TCP的小结
前言:TCP学习的综述 在学习TCP/IP协议的大头:TCP协议 的过程中,遇到了很多机制和知识点,详解中更是用了足足8章的内容介绍它. TCP协议作为 应用层 和 网络层 中间的 传输层协议,既要为 ...
- UVa 1374 快速幂计算(dfs+IDA*)
https://vjudge.net/problem/UVA-1374 题意:给出n,计算最少需要几次能让x成为x^n(x和已经生成的数相乘或相除). 思路:IDA*算法. 如果当前数组中最大的数乘以 ...
- 通过gevent实现【单线程】下的多socket并发
server import sys import socket import time import gevent from gevent import socket,monkey monkey.pa ...
- Codeforces 768B - Code For 1(分治思想)
768B - Code For 1 思路:类似于线段树的区间查询. 代码: #include<bits/stdc++.h> using namespace std; #define ll ...