Introduction#
Nowadays, large-scale numerical calculations or training machine learning models can take hours or even days. Naturally, we want to receive notifications when the process is complete, so we don't have to keep checking.
Surprisingly, there are very few relevant tutorials online. This article provides the usage methods of two free push services, "bark" and "wxpusher", to automatically send notifications to your phone or WeChat when the script finishes running.
bark#
bark is a free and open-source push service that only supports iOS.
Download and Installation#
After entering the app, simply copy the link shown in the image above.
Python Implementation#
First, you need the requests package.
import requests
Add the following line after the code block for model training:
ret = requests.get('https://api.day.app/8BZtwxVav***********/AlarmDingDingDing/TestMessage')
Replace the link above with your own and modify the content as desired.
Once the program reaches this line, a notification will be sent to your phone.
wxpusher#
If you have an Android phone, you can use wxpusher to push messages to WeChat. It is currently completely free. A similar and well-known service called "Server 酱" now only provides 5 free pushes per day, so it is not recommended.
Create an Application#
Simply scan the QR code with WeChat to register automatically. After entering, create an application by filling in the required fields. Once created, you will be given an appToken, so be sure to save it. The format is as follows:
AT_yn7Xsvz**********
After creating the application, a QR code will be displayed. Scan it with WeChat to associate yourself with the application. Then click on the user list on the left and copy your UID.
The format of the UID is as follows:
UID_VO8eFt***********
Python Implementation#
Compared to "bark" mentioned above, the WeChat API has some restrictions and requires data transmission in JSON format.
import requests
import json
# wxpusher
headers = {'content-type': "application/json"}
body = {
"appToken":"AT_yn7Xsvz**********",
"content":"This is a test message",
"summary":"AlarmDingDingDing",
"contentType":1,
"topicIds":[],
"uids":["UID_VO8eFt***********"]
}
Replace "appToken" and "uids" with your own.
Add the following lines after the code block for model training:
ret = requests.post('http://wxpusher.zjiecode.com/api/send/message', data=json.dumps(body), headers=headers)
This will send a POST request to the API, immediately sending a notification to your WeChat when the program finishes running.
Final Result#
It's very fast, and you'll receive a notification almost immediately after executing the program.
The only downside is that due to some restrictions imposed by WeChat, the notification cannot directly display the content and needs to be clicked to view.
Advanced#
- Not only suitable for model training, but can also be used for any long-running program.
- Can be further developed, for example, sending notifications when exceptions occur during program execution.
- This article only provides Python examples, but it can also be applied to MATLAB (refer to here) or other scripting languages as long as they can send HTTP requests.