DEV Community

Cover image for How to Get Notified When Your Python Script Has Finished Executing
Code_Jedi
Code_Jedi

Posted on

How to Get Notified When Your Python Script Has Finished Executing

Often when it comes to tasks such as AI/ML, Web Scraping, Data Mining, Web Automation and more with Python, they can take a lot of time to execute, making you check if it has finished executing every couple of minutes.

Today, I'll show how you can make your Python script notify you when it has finished executing and if it returned an error or executed successfully.

Plyer

For this to work, you'll need to install plyer using pip3 install plyer. Then import "notification" from plyer at the beginning of your Python script along with sys and time:



import sys
import time
from plyer import notification


Enter fullscreen mode Exit fullscreen mode

The Notification Function

Here's the notification function that will notify you when your code has finished executing:



if __name__ == "__main__":
    while True:
        time.sleep(3)
        notification.notify(
            title = "Finished executing "+sys.argv[0],
            message = "Successful",
        )
        time.sleep(5)
        break


Enter fullscreen mode Exit fullscreen mode

I know the if __name__ == "__main__": and while True: might seem unnecessary, but the notification.notify function can't work unless nested in these. Also, for those wondering, sys.argv[0] gets the name of your script.

Implementation

Here's a sample code that implements this method by running for 30 seconds then notifying you once it has finished executing.



import sys
import time
from plyer import notification

if __name__ == "__main__":
    print("Doing stuff...")
    time.sleep(30)
    while True:
        time.sleep(3)
        notification.notify(
            title = "Finished executing "+sys.argv[0],
            message = "Successful",
        )
        time.sleep(5)
        break



Enter fullscreen mode Exit fullscreen mode

Here's how the notification will look like:
Notification

What if it didn't execute successfully?

If your script returns an error, we can simply use try/except like so:



import sys
import time
from plyer import notification

if __name__ == "__main__":
    try:
        print("Doing stuff...")
        time.sleep(5)
        while True:
            time.sleep(3)
            notification.notify(
                title = "Finished executing "+sys.argv[0],
                message = "Successful",
            )
            time.sleep(5)
            break

    except:
        while True:
            time.sleep(3)
            notification.notify(
                title = "Finished executing "+sys.argv[0],
                message = "Failed",
            )
            time.sleep(5)
            break



Enter fullscreen mode Exit fullscreen mode

Notification 2

Conclusion

Hopefully using this method you can now peacefully browse memes while your Python code is executing without checking it every 5 minutes ;)

Byeeee👋

Top comments (0)