bugrobot用6行代码调用微信报警机器人
共 3855字,需浏览 8分钟
·
2021-11-20 21:55
算法工程师常常会为监控代码而头痛,有些代码执行时间常常会长达数小时,甚至几天。
例如一些机器学习模型的训练,以及一些大数据ETL任务。
如果能够将执行过程中的一些中间重要信息发送到我们的微信上,随时随地在手机上看到程序是否正常运行,让一切都在掌握之中,
那么将会让算法工程师的许多头发免受骨肉分离,随风飘零之苦。
实际上我们通过利用Python代码发送邮件到我们的QQ邮箱,并在微信上设置QQ邮箱提醒,可以非常容易实现这个功能。
效果如下,让我们来看看怎么做吧!
Algorithm engineers or RDs often feel anxious when monitoring their running codes.
Some codes can be running for several hours and even be running for several days.
Such as the training of Deep Learning Models and some Big Data ETL tasks.
If we can send some important messages about the running status to our email or wechat,
then we can monitor our running task from our phone.
Wow, that will be a great bless to the rest hairs of our algorithm engineers.
Actually, this can be achieved easily by send email with Python.
If we set QQ mail remind on wechat and send emails to our QQ Email, we cat receive the messages on our phone wechat.
Let's see how to do this!
一,微信设置QQ邮箱提醒
微信上点击 我->设置->通用->辅助功能->QQ邮箱提醒 开启即可。
二,用Python自动发送邮件
import smtplib
from email.mime.text import MIMEText
subject = "info@train_model.py" #邮件主题
msg = "auc=0.98" #邮件内容
receivers = ["2650115830@qq.com"] #收件人
#设置服务器所需信息
mail_host = 'smtp.yeah.net'
mail_user = 'PyRobot'
mail_pass = 'XDNCKUHPLRQFVQHA' #密码(部分邮箱为授权码)
sender = 'PyRobot@yeah.net'
message = MIMEText(msg,'plain','utf-8')
message['Subject'] = subject
message['From'] = sender
message['To'] = receivers[0]
#登录并发送邮件
try:
smtpObj = smtplib.SMTP()
#连接到服务器
smtpObj.connect(mail_host,25)
#登录到服务器
smtpObj.login(mail_user,mail_pass)
#发送
smtpObj.sendmail(
sender,receivers,message.as_string())
#退出
smtpObj.quit()
print('success')
except smtplib.SMTPException as e:
print('error',e) #打印错误
三,封装成通用函数
可以将以上代码封装成函数
import smtplib
from email.mime.text import MIMEText
subject = "info@train_model.py" #邮件主题
msg = "auc=0.98" #邮件内容
receivers = ["2650115830@qq.com"] #收件人
def send_msg(receivers,subject,msg=""):
#设置服务器所需信息
mail_host = 'smtp.yeah.net'
mail_user = 'PyRobot'
mail_pass = 'XDNCKUHPLRQFVQHA' #密码(部分邮箱为授权码)
NPWPJBSIVXRTYUOB
sender = 'PyRobot@yeah.net'
#构造邮件内容
message = MIMEText(msg,'plain','utf-8')
message['Subject'] = subject
message['From'] = sender
message['To'] = receivers[0]
#登录并发送邮件
try:
smtpObj = smtplib.SMTP()
#连接到服务器
smtpObj.connect(mail_host,25)
#登录到服务器
smtpObj.login(mail_user,mail_pass)
#发送
smtpObj.sendmail(
sender,receivers,message.as_string())
#退出
smtpObj.quit()
return 'send_msg success'
except smtplib.SMTPException as e:
error = 'send_msg error : '+str(e)
print(error)
return error
send_msg(receivers,subject,msg)
有时候我们的代码执行过程中可能会出现一些预料之外的错误,我们希望能够在出错的第一时间内收到出错的消息和原因,
可以使用traceback捕获异常,并发送报错堆栈到我们的微信上。
import traceback
try:
print("train model start")
x = 100/0
print("train model start")
except Exception as e:
error_msg = traceback.format_exc()
send_msg(["2650115830@qq.com"],"error@train_model.py",error_msg)
raise e
可以将以上代码也封装成函数
import traceback
def monitor_run(function,receivers):
try:
function()
except Exception as e:
error_msg = traceback.format_exc()
send_msg(receivers,"error@"+function.__name__,error_msg)
raise e
def f():
return 1/0
monitor_run(f,["2650115830@qq.com"])
四,函数使用范例
上述函数代码已经封装在了python库 bugrobot 文件中了,可以用pip安装使用。
!pip install bugrobot
import bugrobot
subject = "info@train_model.py"
msg = "auc=0.98"
receivers = ["2650115830@qq.com"]
#①send message to Email!
bugrobot.send_msg(receivers,subject,msg)
#②send the bug information to Email if any exception occor!
def f():
return 1/0
bugrobot.monitor_run(f,["2650115830@qq.com"])
公众号后台回复关键字: bugrobot,获取项目github项目地址。
万水千山总是情,点个再看行不行!😋 😋