pythonterminal-servicescitrix

Technique to limit number of instances of our application under Terminal Server


I'm looking for simple ways to monitor and limit the number of instances of our application under Terminal Server (2003 and 2008).

The purpose of this restriction is to make sure we don't overload our servers. This is an internal administrative requirement - I am not looking for a licensing solution.

The application in question is written in Python 2.6 (32-bit) but I'm happy to receive development tool agnostic answers. Although we are not using Citrix, I am happy to receive Citrix related ideas with the hope that I can use a similar technique with Terminal Server.


Solution

  • The various instances of your application need some way to communicate with one another. When an instance starts up, it asks the question, 'how many are already running?'. If there are more than the allowed n, it chooses not to start up.

    One implementation approach might be to make n files to lock for the n instances of your application you allow to run at the same time. Then, the application tries to get a lock on one of those files; if it can't, it exits immediately. Release the lock when you're done, but presumably the OS would release the lock for you if you crash.

    Another approach would be to register some per-process unique piece of information (a PID?) in a central location (some database) when you start.

    Yet a third might be to use a host-only network server where either a dedicated server program or one of the instances coordinates communications with the other instances. If the host exits, one of the other instances can self-promote to being the server.