4

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.

1
  • Store the configuration as a dictionary then just dump and load via json. Commented May 10, 2011 at 11:50

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I upvoted this solution too but I think my answer requires less coding to achieve same result.
If I was running this script every 5-10 seconds, would closing the DB still make sense?
@Hanpan: closing the SQLite DB is a must, I think, regardless of your period. Anyhow, 5-10 seconds is eternity, relatively, so don't worry about it
3

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

1 Comment

Depending on the size of the file, the OS / file system and other factors, I'd say this is a recipe for race conditions. I recommend a simple DB instead of a plain file
2

If you're using Linux you can try pyinotify. Example here.

Comments

1

I agree with track data stored in a file then use signal module to inform your script that new track data is ready ro be read, tie a function to, say SIGUSR1, and you're done, no risk of partially-written files.

In your script put:

import signal

signal.signal(signal.SIGUSR1, read_track_data)

then, (linux way but would be much different in Windows) just send signal to your script just after updating your track data file.

$kill -n 10 PID_OF_YOUR_SCRIPT

5 Comments

I would love to see the "not much different in Windows" solution ;-)
@Eli: that's why I love Linux ^^
I love Linux too, but you should really delete this claim unless you're ready to stand behind it
@Eli: I edited the answer, I was not implying that is possible to send signals like `SIGUSR1' in Windows, simply that there are alternatives there like Win32 messages, Remote Process Calls or others. I agree it's not as simple.
OK. Just for the record, on Windows I wouldn't follow this road. If I had something against polling, I would use socket notification. It's also not too much code with Python
0

You can communicate both scripts using sockets

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.