0

I have a game server implemented in Python to which clients can connect and play against each other. I'd like to be able to reload configuration from a config file without restarting the server (as that would disconnect all clients). More specifically, I want to run a command which somehow tells the running server process that it should reload the config (or rather do some generic action, doesn't need to be specific to configuration reloading).

How is this usually implemented? I think it is a pretty common functionality but when searching, I only find questions about how to make a specific application XY reload but nothing on how to best implement this in my own application.

One option would be to monitor the config file for changes and automatically reload, but I would prefer to trigger the reloading manually.

I'm on Linux by the way and the solution doesn't need to be portable.

5
  • 1
    Telling a running program to perform some action is usually done by sending a signal to the underlying process and programming a signal handler in the code to do whatever the appropriate action is. Since this is more OS-dependent than language-dependent, you should probably tag your question with the platform you're using.CommentedMar 12 at 9:49
  • @KilianFoth I see. So I guess on Linux I would use SIGUSR1 or 2 for this?
    – luator
    CommentedMar 12 at 10:04
  • The official documentation says "set aside for you to use any way you want", so apparently yes.CommentedMar 12 at 10:57
  • By convention, SIGHUP is also frequently used to indicate that a non-interactive program should reload its config. But in practice, you can use any IPC mechanism. Signals, flags in shared memory, watching files, sockets, …
    – amon
    CommentedMar 12 at 13:06
  • Keeping it as a comment because my experience is neither with Python nor Linux, but the .NET way of doing this on Windows is to have a file watcher, which sends out triggers whenever a specific (watched) file on disk gets updated. It's always possible to fix this yourself by doing a polling-style approach where you make your own trigger to reload the file, but if there's a way to hook into an existing (and more accurate) trigger, I'd suggest going that way.
    – Flater
    CommentedMar 13 at 1:11

2 Answers 2

1

One way to do it is through signals. Signals is the mechanism that the operating system uses to communicate to the process that it should do something specific—for instance, terminate—that's SIGKILL, or its nicer brother, SIGTERM.

Python (as you tagged your question ) is able to handle those signals. For instance, here's a very simple piece of code that would react when SIGQUIT is received:

#!/usr/bin/env python import time import signal def handle_signal(_, _2): print("Received SIGQUIT.") signal.signal(signal.SIGQUIT, handle_signal) while True: time.sleep(1) 

If you run it, you can find its PID by doing (replace ellipsis by the actual name of your script):

ps -aux | grep "..." 

Say the PID is 6570, then by doing:

kill -s QUIT 6570 

you'll see “Received SIGQUIT.” in the console (and the process will still continue).

This is exactly the approach that is used by Nginx, for example, to re-open log files, or to re-read configuration. Naturally, as Nginx uses USR1 and HUP for this, it's up to you to come with proper signals for the reloading of configuration among all the signals that are available—using a signal such as SIGQUIT would be rather counter-intuitive.

How exactly you handle those signals is up to you, and strongly depends on the architecture of your application. For instance, if you're relying on multiprocessing, you'll use multiprocessing.Event to inform the different processes that configuration changed.

As you correctly noted, listening to file changes (Linux has inotifywait for that, but Python may have more convenient tools for that) is also a valid approach. Microsoft's web applications in .NET used this approach for decades, and it works rather well.

    -1

    The best approach is to watch for configuration changes and apply them dynamically without restarting the server. Common methods include:

    File Watchers – Use libraries like inotify (Linux) or watchdog (Python) to detect changes in config files and reload them.

    Signal Handling – Implement a signal handler (e.g., SIGHUP in Unix) to reload configurations when triggered.

    Hot Reload via API - Provide an endpoint or admin interface to reload configurations on demand.

    Config Service – Store configurations in a central database or service and poll for updates periodically.

    The best method depends on your tech stack and application needs.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.