If your database is Postgresql
you can put a PL/Python function right inside the database - the documentation is here:
https://www.postgresql.org/docs/9.0/static/plpython.html
I don’t know if Python inside postgresql will have limitations (like filesystem access, importing third-party modules that don’t have to do with postgres, etc...) but it probably has no limitations: it is very difficult to create effective limitations of this kind in Python - and to have a "appearance" of security, it is better to document that functions in Python can do everything, and increase the range of choices.
If the system you insert into the database is in Python
That brings us to something else: if the system is inserting in the for database in Python, then the best Python Orm (like Sqlalchemy) has an event system - you would harvest your Python code as an Handler for Sqlalchemy event.
If it is neither Postgresql nor Python insertion:
You will need to send a signal out of the bank somehow from scripts used as Trigger within the bank in other languages. This would require a lot of specific configuration, and could be problematic when the database is on a separate machine from your Python script (Let’s assume that you put a Rigger to make a socket call, which activates your Python script - when it’s in production and the network gets to the bank set up securely, it would be a headache to have this call from the database back to your server where the Python script is).
So, the idea of having a Python script that actively looks at the database at each short interval of time is not bad at all. What you can do instead of doing a long query that looks at the entire table is:
- create a simple table, just to tell if there has been change or not -
could be a table with a únic align and a single column type "bool(changed)"
- Place a Rigger in the bank to set an "altered=true" in that table each time one of the changes you are interested in happening.
- Put your script looking at the database every 1 or 2 seconds, checking this simple table - and only do more extensive queries if there has been a change
Search on Trigger for database, can help you
– Marcelo Diniz
Which database you are using?
– José Henrique Luckmann
I use Mysql.
– BrunoVdutra
https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
– Leandro
Is it possible to make a Trigger call an external python script? From what I researched it only does internal actions with sql command.
– BrunoVdutra
probably not: https://stackoverflow.com/questions/1467369/invoking-a-php-script-from-a-mysql-trigger
– Leandro
Vlw, I think we can do with UDF library.
– BrunoVdutra
If your server is Linux, you can create a Cron to run your scripts. In windows also has something similar, is the Task Scheduler, just configure it and it runs something from time to time.
– Roberto de Campos
Switch to Postgresql and use PL/Python to develop the Trigger function.
– Anonimo