Python - setting update while the script is running

I have a python script that is constantly processing data. The script is constantly running and should not stop.

The script checks the data from the keyword track, which is 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 save the track in a txt file and check for any file updates by the given timer. It seems to be messy.

+4
source share
5 answers

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

The advantage of the database is that you will not encounter the conditions of the race for partially written files, etc. The "add configuration" script adds keywords using a transaction, and another script, counting from the database, will see this only when it is fully executed. Just remember to always keep the DB in a batch script. After some time, open it, read the keywords, and close it.

+5
source

Polling the configuration file is not confusing, but a very common solution to this problem. You have to go with him.

+3
source

If you are using Linux, you can try pyinotify . An example is here .

+2
source

I agree with the track data stored in the file, and then the signal module to inform your script that the new track data is ready to be read, attach a function, say, SIGUSR1, and everything is ready, without the risk of partially written files.

In the script, put:

import signal signal.signal(signal.SIGUSR1, read_track_data) 

then (linux way, but will be very different on Windows) just send a signal to your script right after updating the track data file.

 $kill -n 10 PID_OF_YOUR_SCRIPT 
+1
source

You can pass both scripts using sockets

0
source

All Articles