In this blogpost, I will show how you can make your python scripts notify you for the progress of your training, through Slack messages.
Slack is a popular tool for team communication that offers some very nice utilities. One of this is the ability to forward emails sent to a specific email address. As shown following gif, all you have to do is enable this functionality in Project Preferences > Message and Media > Get a Forwarding Email Address.
A seemingly random forwarding email address will be assigned to you by Slack. Every email you send to that address will appear as a message in your chat with the slack bot.
You can leverage this utility to send automatic updates from your python code regarding the progress of long-running training scripts. I especially like to use this notification messages for updates related to unexpected events that require action, like code crashes. It’s pretty devastating to start training on Friday only to see 2 days after that something went wrong 2 hours later. Moreover, the use of slack helps me avoid polluting my email inbox.
The following python code can be easily added to a try-catch statement that wraps my code
import smtplib # Import the email modules we'll need from email.mime.text import MIMEText try: #trainning code except Exception as error: # Open a plain text file for reading. For this example, assume that # the text file contains only ASCII characters. msg = MIMEText("Trainning Message!") msg['Subject'] = 'Update for Experiment : Experiment1' msg['From'] = 'email@example.com' #the slack email msg['To'] = "firstname.lastname@example.org" # Send the message via our own SMTP server. Depending on your mailing servers configuration different #methods might need to be invoked. Here i have to use SMTP_SSL s = smtplib.SMTP_SSL('smtp.nottingham.ac.uk') s.login("yourUserName", "yourPassword) s.send_message(msg) s.quit()
The code snipper above works for my configuration, but it can easily be modified for gmail:
server = smtplib.SMTP('smtp.gmail.com', 587)