It is not uncommon for one program to take several days to execute when doing machine learning. I am worried about the execution status of the program and open the terminal every few hours. Do you not spend such days? For such a person, this time I will introduce a method to notify the error and execution completion of the Python program by LINE. If you have a LINE account, you can do it in about 10 minutes, so be sure to check it out!
We use a service called LINE Notify provided by LINE to send notifications.
First, issue a token from here. https://notify-bot.line.me/my/
After logging in to your LINE account from the login button on the upper right, issue and copy the token according to the following procedure.
Set the token name as you like. This time, it is called "execution result notification".
Be sure to copy it here.
This completes the preparation for LINE Notify.
All you have to do is copy and paste the following program and change the token part.
(Maybe you need pip install requests
)
line_notify.py
import requests
#Function to notify LINE
def line_notify(message):
line_notify_token = 'Paste the token here'
line_notify_api = 'https://notify-api.line.me/api/notify'
payload = {'message': message}
headers = {'Authorization': 'Bearer ' + line_notify_token}
requests.post(line_notify_api, data=payload, headers=headers)
if __name__ == '__main__':
message = "Hello world!"
line_notify(message)
When you run python line_notify.py
, you should get a message" Hello world! "On LINE.
After that, just notify it in combination with exception handling. Try running the following program.
hoge.py
import requests
#Function to notify LINE
def line_notify(message):
line_notify_token = 'Paste the token here'
line_notify_api = 'https://notify-api.line.me/api/notify'
payload = {'message': message}
headers = {'Authorization': 'Bearer ' + line_notify_token}
requests.post(line_notify_api, data=payload, headers=headers)
# a/Function to calculate b
def foo(a, b):
return a / b
if __name__ == '__main__':
try:
ans = foo(1, 0)
except Exception as e:
line_notify(e)
else:
line_notify("finished")
Change foo (1, 0) to foo (1, 1) and execute it. You have been notified correctly.
Recommended Posts