X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=bin%2FPortManager.py;h=8e7a31d63056a41b705050451153fff008522239;hb=f6f9c5bd0eb17ab1f0cb40d4761995f48f093526;hp=d820a855570930c840a0f1adb5b9b6b3d041d34e;hpb=653d82fd45a52d7f44b93beb65eb2825b1d1c8ee;p=modules%2Fkernel.git diff --git a/bin/PortManager.py b/bin/PortManager.py index d820a8555..8e7a31d63 100644 --- a/bin/PortManager.py +++ b/bin/PortManager.py @@ -8,7 +8,7 @@ # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either -# version 2.1 of the License. +# version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -21,25 +21,14 @@ # # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com # -from Singleton import Singleton - -import multiprocessing -import time -import socket - import os import sys -import threading -import SocketServer try: import cPickle as pickle except: import pickle -import struct -import ctypes - import logging def createLogger(): logger = logging.getLogger(__name__) @@ -53,42 +42,24 @@ def createLogger(): # logger = createLogger() - -if sys.platform == 'win32': - import multiprocessing.reduction # make sockets pickable/inheritable - -multiprocessing.freeze_support() # Add support for when a program which uses multiprocessing has been frozen to produce a Windows executable. - #------------------------------------ # A file locker (Linux only) -import fcntl -class PortManagerLock: - def __init__(self, filename, readonly=False, blocking=True): - # This will create it if it does not exist already - logger.debug("Create lock on %s"%filename) - self.__readonly = readonly - self.__blocking = blocking - self.__filename = filename - flag = 'w' - if self.__readonly: - flag = 'r' - self.handle = open(self.__filename, 'a+') - - def acquire(self): - mode = fcntl.LOCK_EX - if not self.__blocking: # Raise an IOError exception if file has already been locked - mode = mode | fcntl.LOCK_NB - fcntl.flock(self.handle, mode) - logger.debug("lock acquired %s"%self.__blocking) - - def release(self): - fcntl.flock(self.handle, fcntl.LOCK_UN) - logger.debug("lock released") - - def __del__(self): - logger.debug("Close lock file") - self.handle.close() - os.remove(self.__filename) +def __acquire_lock(lock): + if sys.platform == "win32": + import msvcrt + # lock 1 byte: file is supposed to be zero-byte long + msvcrt.locking(lock.fileno(), msvcrt.LK_LOCK, 1) + else: + import fcntl + fcntl.flock(lock, fcntl.LOCK_EX) +# +def __release_lock(lock): + if sys.platform == "win32": + import msvcrt + msvcrt.locking(lock.fileno(), msvcrt.LK_UNLCK, 1) + else: + import fcntl + fcntl.flock(lock, fcntl.LOCK_UN) # def _getConfigurationFilename(): @@ -134,7 +105,7 @@ def getPort(preferedPort=None): config_file, lock_file = _getConfigurationFilename() with open(lock_file, 'w') as lock: # acquire lock - fcntl.flock(lock, fcntl.LOCK_EX) + __acquire_lock(lock) # read config config = {'busy_ports':[]} @@ -171,7 +142,7 @@ def getPort(preferedPort=None): pass # release lock - fcntl.flock(lock, fcntl.LOCK_UN) + __release_lock(lock) logger.debug("get port: %s"%str(port)) return port @@ -184,7 +155,7 @@ def releasePort(port): config_file, lock_file = _getConfigurationFilename() with open(lock_file, 'w') as lock: # acquire lock - fcntl.flock(lock, fcntl.LOCK_EX) + __acquire_lock(lock) # read config config = {'busy_ports':[]} @@ -213,7 +184,7 @@ def releasePort(port): pass # release lock - fcntl.flock(lock, fcntl.LOCK_UN) + __release_lock(lock) logger.debug("released port port: %s"%str(port)) # @@ -222,7 +193,7 @@ def getBusyPorts(): config_file, lock_file = _getConfigurationFilename() with open(lock_file, 'w') as lock: # acquire lock - fcntl.flock(lock, fcntl.LOCK_EX) + __acquire_lock(lock) # read config config = {'busy_ports':[]} @@ -237,7 +208,7 @@ def getBusyPorts(): busy_ports = config["busy_ports"] # release lock - fcntl.flock(lock, fcntl.LOCK_UN) + __release_lock(lock) return busy_ports #