Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - update configuration while running script

Tags:

python

I have a python script which is constantly polling data. The script is constantly running and should never stop.

The script polls data from a track of keywords which are passed to it when the script is first run.

What would be the best way to update this track without stopping the script from another python script?

The only solution I can think of is to store the track in a txt file and check for any updates to the file on a set timer. Seems kind of messy.

like image 535
Hanpan Avatar asked Dec 21 '22 14:12

Hanpan


2 Answers

It's better to encapsulate this settings file in a database. A simple SQLite DB file is enough - SQLite support is built-in with Python so no extra effort is required.

The advantage of a DB is that you won't run into race conditions of partially-written files, etc. The "configuration-adding" script adds keywords using a transaction, and the other script reading from the DB will only see it when it's wholly done. Just remember to not hold the DB open all the time in the periodic script. Once every some time, open it, read the keywords, and close it.

like image 189
Eli Bendersky Avatar answered Jan 08 '23 04:01

Eli Bendersky


Polling a configuration-file is not messy, but a very common solution to this problem. You should go with it.

like image 37
Björn Pollex Avatar answered Jan 08 '23 05:01

Björn Pollex