Make notifications with Slack API when python experiment is done

Satoshi Miyazawa
Aug 25, 2017 · 2 min read

Background

I work on Deep Learning models it involves running experiment on external remote server for more power than my local laptop. The experiments usually take a few hours, so I wanted some system that notifies me when the experiment finishes. Luckily, I use Slack (in this example, my personal Slack “team”) and I found you can make message posted to a channel using Python API client.

Objective

Send custom messages to Slack channel when Python scripts finish.

Method

Step 1: Install Slack Python client with Pypi (https://slackapi.github.io/python-slackclient/)

pip install slackclient

Step 2: Create test token (follow: http://slackapi.github.io/python-slackclient/auth.html#handling-tokens )

You can create a legacy token on this page (https://api.slack.com/custom-integrations/legacy-tokens). The legacy token is not recommended for production usage, but I found it easiest and sufficient for personal use.

Step 3: Prepare source code

Instead of writing the token in the source code, Slack’s documentation recommends you to pass the token as environmental variables. I use configparser module instead.

Let’s assume we have three files in our project directory. config.cfg is your config file, settings.py is your experiment setting file, experiment.py is you experiment file (setting.py will be imported first).

Project
- config.cfg
- settings.py
- experiment.py

In config.cfg, you write your token (make sure you add the file to your .gitignore).

[Slack]
token = xxx

In settings.py, you load the token and define the notification function. You can choose which channel the notification will appear in.

import configparser
conf = configparser.ConfigParser()
conf.read('config.cfg')
SLACK_TOKEN = conf.get('Slack', 'token')
sc = SlackClient(SLACK_TOKEN)
def exit_handler(finename):
sc.api_call("chat.postMessage", channel="#experiment",text=finename + " is finished.")

In experiment.py, you have your experiment code. At the end of the script, you call the “exit_handler” function to make the notification.

import atexit
import
settings as s
# Do your experiment# Make notification
atexit.register(s.exit_handler, __file__)

Result

You’ll see notifications once your Python script finish.

At this moment, it only makes notifications when “Process finished with exit code 0”, meaning the script finishes without error. I’m looking for other way to make notification also when the script finishes with error.

)

Satoshi Miyazawa

Written by

Project Researcher at Center for Spatial Information Science (CSIS), the University of Tokyo. Working on city-scale human mobility prediction with GPS and SNS.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade