Event listener in python script on a server
Question: I want to write a python script to run on a server that will be checking for
changes on a database (e.g. total number of records), and when one occurs it
will perform an action.

I am new in python and I don't know how I should approach this, is there a
proposed event listening methodology to optimize cpu consumption and ensure
smooth running?

I'm not asking for specific code but more like the high-level idea.

EDIT: After KT.'s helpful response I built this code to implement a listener
for changes in a json response from a URI api:

    
    
    import json
    import time
    import requests
    
    class Test():
    
        def __init__(self):
            self.total_services = self.__get_total_services()
    
        def __get_total_services(self):
            url = "my_url_that_responds_with_json"
            response = requests.get(url)
            json_data = response.json()
            dict_data = json.loads(json.dumps(json_data))
            total = len(dict_data['services'])
            return total
    
        def listen_for_changes(self):
            while 1:
                if (self.__get_total_services() != self.total_services):
                    self.total_services = self.__get_total_services()
                    """do something"""
                time.sleep(60)
            return 0
    
    
    
    my_test = Test()
    my_test.listen_for_changes()
    

My question is should this code run somehow in the background or it is ok to
run it like this from my server?


Answer: In general, there are two ways you can detect changes in the database (or
anywhere else, in fact):

  * **Polling** \- query the database regularly from your code and detect changes in your Python code. This approach is simple and straightforward, although it may indeed be a bit wasteful on the resources, if you poll too frequently.

  * **Notifications** \- configure the database to track changes and notify your application. This method might seem more reasonable in terms of resource usage, but it is may turn out to be so annoying and messy to implement that you would not want to go this way unless you are forced to. In the most general case this approach will require you to:

    * Decide on a way to notify your application (e.g. keep a network socket open, listen to OS signals, track an open file or a pipe, etc).
    * Implement a [trigger](http://dev.mysql.com/doc/refman/5.1/en/faqs-triggers.html) in the database that will react to row insertions or deletions and invoke the notification process (e.g. execute a script that will send a message to your server).
    * If your application and the database server run on different machines, you might need to think about having a queue inbetween to make sure the notifications are not lost in transit.

Alternatively, if your database is only changed from one other application,
you can configure _that_ application to send you notifications whenever
something changes. If your database does not support triggers, this may be
your only chance (if any) to implement the notification-based solution.

