bugrobot用6行代碼調(diào)用微信報警機器人
算法工程師常常會為監(jiān)控代碼而頭痛,有些代碼執(zhí)行時間常常會長達數(shù)小時,甚至幾天。
例如一些機器學(xué)習模型的訓(xùn)練,以及一些大數(shù)據(jù)ETL任務(wù)。
如果能夠?qū)?zhí)行過程中的一些中間重要信息發(fā)送到我們的微信上,隨時隨地在手機上看到程序是否正常運行,讓一切都在掌握之中,
那么將會讓算法工程師的許多頭發(fā)免受骨肉分離,隨風飄零之苦。
實際上我們通過利用Python代碼發(fā)送郵件到我們的QQ郵箱,并在微信上設(shè)置QQ郵箱提醒,可以非常容易實現(xiàn)這個功能。
效果如下,讓我們來看看怎么做吧!
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!

一,微信設(shè)置QQ郵箱提醒
微信上點擊 我->設(shè)置->通用->輔助功能->QQ郵箱提醒 開啟即可。
二,用Python自動發(fā)送郵件
import?smtplib
from?email.mime.text?import?MIMEText
subject?=?"info@train_model.py"?#郵件主題
msg?=?"auc=0.98"?#郵件內(nèi)容
receivers?=?["[email protected]"]?#收件人
#設(shè)置服務(wù)器所需信息
mail_host?=?'smtp.yeah.net'??
mail_user?=?'PyRobot'??
mail_pass?=?'XDNCKUHPLRQFVQHA'???#密碼(部分郵箱為授權(quán)碼)?
sender?=?'[email protected]'??
message?=?MIMEText(msg,'plain','utf-8')??
message['Subject']?=?subject
message['From']?=?sender?????
message['To']?=?receivers[0]??
#登錄并發(fā)送郵件
try:
????smtpObj?=?smtplib.SMTP()?
????#連接到服務(wù)器
????smtpObj.connect(mail_host,25)
????#登錄到服務(wù)器
????smtpObj.login(mail_user,mail_pass)?
????#發(fā)送
????smtpObj.sendmail(
????????sender,receivers,message.as_string())?
????#退出
????smtpObj.quit()?
????print('success')
except?smtplib.SMTPException?as?e:
????print('error',e)?#打印錯誤
三,封裝成通用函數(shù)
可以將以上代碼封裝成函數(shù)
import?smtplib
from?email.mime.text?import?MIMEText
subject?=?"info@train_model.py"?#郵件主題
msg?=?"auc=0.98"?#郵件內(nèi)容
receivers?=?["[email protected]"]?#收件人
def?send_msg(receivers,subject,msg=""):
????
????#設(shè)置服務(wù)器所需信息
????mail_host?=?'smtp.yeah.net'??
????mail_user?=?'PyRobot'??
????mail_pass?=?'XDNCKUHPLRQFVQHA'???#密碼(部分郵箱為授權(quán)碼)?
????NPWPJBSIVXRTYUOB
????sender?=?'[email protected]'??
????#構(gòu)造郵件內(nèi)容
????message?=?MIMEText(msg,'plain','utf-8')??
????message['Subject']?=?subject
????message['From']?=?sender?????
????message['To']?=?receivers[0]??
????#登錄并發(fā)送郵件
????try:
????????smtpObj?=?smtplib.SMTP()?
????????#連接到服務(wù)器
????????smtpObj.connect(mail_host,25)
????????#登錄到服務(wù)器
????????smtpObj.login(mail_user,mail_pass)?
????????#發(fā)送
????????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)
有時候我們的代碼執(zhí)行過程中可能會出現(xiàn)一些預(yù)料之外的錯誤,我們希望能夠在出錯的第一時間內(nèi)收到出錯的消息和原因,
可以使用traceback捕獲異常,并發(fā)送報錯堆棧到我們的微信上。
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(["[email protected]"],"error@train_model.py",error_msg)
????raise?e
????
可以將以上代碼也封裝成函數(shù)
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,["[email protected]"])
四,函數(shù)使用范例
上述函數(shù)代碼已經(jīng)封裝在了python庫 bugrobot 文件中了,可以用pip安裝使用。
!pip?install?bugrobot
import??bugrobot
subject?=?"info@train_model.py"?
msg?=?"auc=0.98"?
receivers?=?["[email protected]"]?
#①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,["[email protected]"])
公眾號后臺回復(fù)關(guān)鍵字:?bugrobot,獲取項目github項目地址。
萬水千山總是情,點個再看行不行!?? ??
