def _unlock_file(f):
fcntl.flock(f, fcntl.LOCK_UN)
python类LOCK_UN的实例源码
def _update(self, mode):
if mode is None:
if self.locked:
fcntl.lockf(self.fd, fcntl.LOCK_UN)
self.locked = False
elif self.mode is not mode or not self.locked:
self.mode = mode
self.locked = True
for offset in self.locks:
fcntl.lockf(self.fd, self.mode, 1, offset)
def unlock(f):
ret = fcntl.flock(_fd(f), fcntl.LOCK_UN)
return (ret == 0)
def unlock_and_close(self):
"""Close and unlock the file using the fcntl.lockf primitive."""
if self._locked:
fcntl.lockf(self._fh.fileno(), fcntl.LOCK_UN)
self._locked = False
if self._fh:
self._fh.close()
def _unlock(lockfile):
fcntl.lockf(lockfile, fcntl.LOCK_UN)
def unlock(file):
fcntl.flock(file.fileno(), fcntl.LOCK_UN)
def release(self):
fcntl.flock(self.handle, fcntl.LOCK_UN)
def unlock(f):
ret = fcntl.flock(_fd(f), fcntl.LOCK_UN)
return (ret == 0)
def run(self):
"""
THIS IS BLOCKING! CALL IT AT LAST!
"""
with open(Job.lockFileName, 'w') as f:
rv = fcntl.lockf(f.fileno(), fcntl.LOCK_EX)
print("job {} is running.".format(os.getpid()))
f.write(str(os.getpid()) + '\n')
f.flush()
self.action()
fcntl.lockf(f.fileno(), fcntl.LOCK_UN)
def write_server_info(filename, port):
pid = os.getpid()
rank = MPI.COMM_WORLD.Get_rank()
server_info = '{}:{}:{}:{}:{}'.format(LINE_TOKEN, rank, pid, port, LINE_TOKEN).strip()
logger.debug("write_server_info: line %s, filename %s", server_info, filename)
time.sleep(0.1 * rank)
with open(filename, "a") as f:
fcntl.lockf(f, fcntl.LOCK_EX)
f.write(server_info + '\n')
f.flush()
os.fsync(f.fileno())
fcntl.lockf(f, fcntl.LOCK_UN)
return server_info
def get_rpc_port_by_rank(self, rank, num_servers):
if self.mpirun_proc is None:
raise RuntimeError("Launch mpirun_proc before reading of rpc ports")
if self._rpc_ports is not None:
return self._rpc_ports[rank]
server_info_pattern = re.compile("^" + LINE_TOKEN +
":([\d]+):([\d]+):([\d]+):" +
LINE_TOKEN + "$")
self._tmpfile.seek(0)
while True:
fcntl.lockf(self._tmpfile, fcntl.LOCK_SH)
line_count = sum(1 for line in self._tmpfile if server_info_pattern.match(line))
self._tmpfile.seek(0)
fcntl.lockf(self._tmpfile, fcntl.LOCK_UN)
if line_count == num_servers:
break
else:
time.sleep(0.1)
server_infos = [tuple([int(server_info_pattern.match(line).group(1)),
int(server_info_pattern.match(line).group(3))])
for line in self._tmpfile]
server_infos = sorted(server_infos, key=lambda x: x[0])
self._rpc_ports = [row[1] for row in server_infos]
logger.debug("get_rpc_ports: ports (in MPI rank order): %s", self._rpc_ports)
self._tmpfile.close()
return self._rpc_ports[rank]
def _unlock_file(f):
fcntl.flock(f, fcntl.LOCK_UN)
def run_chiboard(self):
pass
import subprocess
from chi import board
from chi.board import CHIBOARD_HOME, MAGIC_PORT
port = None
start = False
cbc = join(CHIBOARD_HOME, CONFIG_NAME)
if os.path.isfile(cbc):
with open(cbc) as f:
import fcntl
try:
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
start = True
fcntl.flock(f, fcntl.LOCK_UN)
except (BlockingIOError, OSError): # chiboard is running
try:
data = json.load(f)
port = data.get('port')
except json.JSONDecodeError:
port = None
else:
start = True
if start:
from chi.board import main
chiboard = main.__file__
subprocess.check_call([sys.executable, chiboard, '--port', str(MAGIC_PORT), '--daemon'])
port = MAGIC_PORT
if port is None:
logger.warning('chiboard seems to be running but port could not be read from its config')
else:
logger.info(f"{self.f.__name__} started. Check progress at http://localhost:{port}/exp/#/local{self.logdir}")
def release(self):
fcntl.flock(self._f, fcntl.LOCK_UN)
self._f.close()
# def scalar_summaries(prefix='', **kwargs):
# vs = [tf.Summary.Value(tag=prefix + '/' + name, simple_value=value) for name, value in kwargs.items()]
# s = tf.Summary(value=vs)
# return s
def __release(self):
self.timer.cancel()
self.timer = None
fcntl.flock(self.fd, fcntl.LOCK_UN)
os.close(self.fd)
def __exit__(self, type, value, tb): # pylint: disable=redefined-builtin
"""Exit RunSingleInstance class
:return: None
"""
try:
if not self.__is_running:
fcntl.lockf(self.__filelock, fcntl.LOCK_UN)
self.__filelock.close()
os.unlink(self.__lockfile)
except Exception as err:
logger.error("Error unlocking single instance file", error=err.message)
def _release_all_locks(self):
filedescriptor = self._filedesc
if filedescriptor is not None:
fcntl.flock(filedescriptor, fcntl.LOCK_UN)
os.close(filedescriptor)
self._filedescriptor.remove()
def unlock(file):
fcntl.flock(file.fileno(), fcntl.LOCK_UN)
def _unlock_file(f):
fcntl.flock(f, fcntl.LOCK_UN)
def _unlock(self):
if not fcntl or not self.ser:
return
fcntl.flock(self.ser.fileno(), fcntl.LOCK_UN)
logging.debug('_unlock')