What is elegant way to do configuration on server app

Ben Finney ben+python at benfinney.id.au
Thu Mar 26 04:59:13 EDT 2015


Jerry OELoo <oyljerry at gmail.com> writes:

> Currently, I can just think out that I put status into a configure
> file, and service schedule read this file and get status value,

That sounds like a fine start. Some advice:

* You may be tempted to make the configuration file executable (e.g.
  Python code). Resist that temptation; keep it *much* simpler, a
  non-executable data format.

  Python's standard library has the ‘configparser’ module
  <URL:https://docs.python.org/3/library/configparser.html> to parse and
  provide the values from a very common configuration file format.
  Use that unless you have a good reason not to.

* Your program can “poll” the configuration file to see whether it has
  changed. At startup, read the config file's modification timestamp
  <URL:https://docs.python.org/3/library/os.html#os.stat_result.st_mtime>.

  Make a part of your event loop (assuming your server runs an event
  loop) that wakes up every N seconds (e.g. every 60 seconds) and
  checkes the file's modification timestamp again; if it's newer, record
  that value for future comparisons, then re-read the file for its
  values.

Hope that helps.

-- 
 \             “For your convenience we recommend courteous, efficient |
  `\                            self-service.” —supermarket, Hong Kong |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list