Commit ecbeef77 authored by Daniel Armbruster's avatar Daniel Armbruster Committed by thomas.forbriger
Browse files

added documentation

This is a legacy commit from before 2015-05-18.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/trunk
SVN Revision: 4336
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent 2cb541c1
......@@ -32,6 +32,18 @@
# 04/01/2012 V1.0 Daniel Armbruster
#
# =============================================================================
"""
Module to handle process IDs (pid) by using a lockfile mechanism. The module
comes along with a PidHandler class. Additionally there is a Exception class
PidLockError which might be caught while announcing a pid.
The path of the lockfile should be configured using the global variables.
Default values are:
---
path = os.path.expanduser("~")
lockfilename = ".lock"
---
"""
import os
import sys
......@@ -65,7 +77,9 @@ class PidLockError(Exception):
class PidHandler:
"""
Implements a handler for a pid. This handler uses a pidlockfile mechanism to
keep a log of the asserted pids.
keep a log of the announced pids. Because of using a lockfile the handler
class should be tread-safe. This means that different instances of handlers
sould be able tp use the same lockfile.
Note that this handler does its work only on *nix platforms properly. Use
inheritance to overload the corresponding functions.
"""
......@@ -81,16 +95,14 @@ class PidHandler:
if self.__access() and not self.__pidValid():
os.remove(self.path)
# wait until pid is not valid anymore
while (self.__pidValid() and pid in PidHandler.registeredPids):
print("process [{0}] active.".format(self.__getpid()))
while (self.__pidValid()):
pass
# create a new pidlockfile
try:
pidfile = open(self.path, 'w')
pidfile.write("{0}".format(pid))
PidHandler.registeredPids.append(pid)
except IOError as err:
raise PidLockError(81, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
raise PidLockError(106, "[Errno "+str(err.errno)+"] "+err.strerror+": " \
+err.filename)
else:
pidfile.close()
......@@ -98,12 +110,11 @@ class PidHandler:
def cancel(self, pid):
"""
Remove a process with pid of the handler. The process only can cancel from
the handler if the process had been announced.
the handler if the process had been announced (and is present in the
lockfile).
"""
print("Process canceled.")
if self.__getpid() == pid:
if self.__getlockpid() == str(pid):
os.remove(self.path)
PidHandler.registeredPids.remove(pid)
def __pidValid(self):
"""
......@@ -122,7 +133,7 @@ class PidHandler:
"""
return os.access(self.path, os.F_OK)
def __getpid(self):
def __getlockpid(self):
if self.__access():
pidfile = open(self.path, 'r')
pidfile.seek(0)
......@@ -137,24 +148,25 @@ class PidHandler:
"""
return os.path.exists("/proc/{0}".format(pid))
registeredPids = []
# -----------------------------------------------------------------------------
# Tests
if __name__ == '__main__':
try:
print(os.getpid())
# create handlers
pidhandler1 = PidHandler()
pidhandler = PidHandler()
# announce pid of popen 1
popen1 = subprocess.Popen('echo "Process 1"', shell=True)
popen1 = subprocess.Popen('echo "Process 1"; exit 0', shell=True)
pidhandler.cancel(popen1.pid)
popen1.wait()
print(popen1.pid)
pidhandler1.announce(popen1.pid)
pidhandler.announce(popen1.pid)
# announce another pid
popen2 = subprocess.Popen('echo "Process 2"', shell=True)
popen2 = subprocess.Popen('echo "Process 2"; exit 0', shell=True)
popen2.wait()
print(popen2.pid)
pidhandler1.announce(popen2.pid)
pidhandler1.cancel(popen1.pid)
popen1.kill()
pidhandler.announce(popen2.pid)
pidhandler.cancel(popen2.pid)
except PidLockError as err:
err.display()
sys.exit()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment