What is elegant way to do configuration on server app

Grant Edwards invalid at invalid.invalid
Thu Mar 26 11:49:30 EDT 2015


On 2015-03-26, Ben Finney <ben+python at benfinney.id.au> wrote:
> 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.

I second the recommendation for configparser for stuff of simple to
moderate complexity.  If it gets too complex for configparser, you may
want to consider JSON, or for even more complex stuff just use plain
Python code in your config file (this can be very powerful and
expressive, but can also be diffucult to implement safely).

> * 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.

That sounds rather Windowsesque.  The more-or-less standard way to do
handle the situation on Unix is to reread the config file when you get
a SIGHUP.

-- 
Grant Edwards               grant.b.edwards        Yow! ONE LIFE TO LIVE for
                                  at               ALL MY CHILDREN in ANOTHER
                              gmail.com            WORLD all THE DAYS OF
                                                   OUR LIVES.



More information about the Python-list mailing list