PidFile.py :  » Web-Frameworks » Webware » Webware-1.0.2 » WebKit » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » WebKit » PidFile.py
import os
import sys
import atexit


class ProcessRunning(Exception):
  pass


def removePidFile(pidfile):
  pidfile.remove()


class PidFile:

  def __init__(self, path):
    self._path = path
    self._createdPID = 0
    if os.path.exists(path):
      try:
        pid = int(open(path).read())
      except (IOError, ValueError, TypeError):
        # can't open file or read PID from file.  File is probably
        # invalid or stale, so try to delete it.
        pid = None
        print "%s is invalid or cannot be opened; " \
          "attempting to remove it." % path
        os.unlink(path) # should we catch errors here?
      else:
        if self.pidRunning(pid):
          raise ProcessRunning()
        else:
          print "%s is stale; removing." % path
          try:
            os.unlink(path)
          except OSError:
            # maybe the other process has just quit
            # and has removed the file.
            pass # try continuing...

    pidfile = open(path, 'w')
    pidfile.write(str(self.currentPID()))
    pidfile.close()

    self._createdPID = 1

    # Delete the pid file when python exits, so that the pid file is
    # removed if the process exits abnormally.
    # If the process crashes, though, the pid file will be left behind.
    atexit.register(removePidFile, self)

  def pidRunning(self, pid):
    if os.name == 'posix':
      try:
        os.kill(pid, 0)
      except OSError, e:
        if e.errno == 3: # No such process
          return 0
      return 1
    else:
      try:
        import win32api
        import win32con
        import pywintypes
        try:
          win32api.OpenProcess(
            win32con.PROCESS_QUERY_INFORMATION, 0, pid)
        except pywintypes.error, e:
          if e[0] == 87: # returned when process does not exist
            return 0
      except ImportError:
        pass # couldn't import win32 modules
      return 1

  def currentPID(self):
    if os.name == 'posix':
      return os.getpid()
    else:
      try:
        import win32api
      except ImportError:
        pass
      if sys.modules.has_key('win32api'):
        return win32api.GetCurrentProcessId()
    return None

  def __del__(self):
    self.remove()

  def remove(self):
    # Only remove the file if we created it. Otherwise attempting to start
    # a second process will remove the file created by the first.
    if self._createdPID:
      try:
        os.unlink(self._path)
      except (AttributeError, OSError):
        pass
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.