提交 2fe02fb0 authored 作者: Frederic's avatar Frederic

pep8

上级 8afdfa27
# Locking mechanism to ensure no two compilations occur simultaneously in the
# same compilation directory (which can cause crashes).
# Locking mechanism to ensure no two compilations occur simultaneously
# in the same compilation directory (which can cause crashes).
from theano import config
import os, random, time, atexit
import socket # only used for gethostname()
import atexit
import os
import random
import socket # only used for gethostname()
import time
import logging
_logger=logging.getLogger("theano.gof.compilelock")
_logger.setLevel(logging.INFO) # INFO will show the the messages "Refreshing lock" message
from theano import config
_logger = logging.getLogger("theano.gof.compilelock")
# INFO will show the the messages "Refreshing lock" message
_logger.setLevel(logging.INFO)
# In seconds, time that a process will wait before deciding to override an
# existing lock. An override only happens when the existing lock is held by
......@@ -82,6 +88,7 @@ def get_lock(lock_dir=None, **kw):
get_lock.start_time = now
get_lock.n_lock += 1
def release_lock():
"""
Release lock on compilation directory.
......@@ -93,6 +100,7 @@ def release_lock():
get_lock.start_time = None
get_lock.unlocker.unlock()
def set_lock_status(use_lock):
"""
Enable or disable the lock on the compilation directory (which is enabled
......@@ -104,6 +112,7 @@ def set_lock_status(use_lock):
"""
get_lock.lock_is_enabled = use_lock
def lock(tmp_dir, timeout=120, min_wait=5, max_wait=10, verbosity=1):
"""
Obtain lock access by creating a given temporary directory (whose base will
......@@ -185,7 +194,7 @@ def lock(tmp_dir, timeout=120, min_wait=5, max_wait=10, verbosity=1):
if not no_display:
msg = "process '%s'" % read_owner.split('_')[0]
_logger.warning("Overriding existing lock by dead %s "
"(I am process '%s')", msg, my_pid)
"(I am process '%s')", msg, my_pid)
get_lock.unlocker.unlock()
continue
if last_owner == read_owner:
......@@ -198,7 +207,7 @@ def lock(tmp_dir, timeout=120, min_wait=5, max_wait=10, verbosity=1):
else:
msg = "process '%s'" % read_owner.split('_')[0]
_logger.warning("Overriding existing lock by %s "
"(I am process '%s')", msg, my_pid)
"(I am process '%s')", msg, my_pid)
get_lock.unlocker.unlock()
continue
else:
......@@ -211,8 +220,9 @@ def lock(tmp_dir, timeout=120, min_wait=5, max_wait=10, verbosity=1):
else:
msg = "process '%s'" % read_owner.split('_')[0]
_logger.info("Waiting for existing lock by %s (I am "
"process '%s')", msg, my_pid)
_logger.info("To manually release the lock, delete %s", tmp_dir)
"process '%s')", msg, my_pid)
_logger.info("To manually release the lock, delete %s",
tmp_dir)
if verbosity <= 1:
no_display = True
time.sleep(random.uniform(min_wait, max_wait))
......@@ -220,8 +230,8 @@ def lock(tmp_dir, timeout=120, min_wait=5, max_wait=10, verbosity=1):
try:
os.mkdir(tmp_dir)
except OSError:
# Error while creating the directory: someone else must have tried
# at the exact same time.
# Error while creating the directory: someone else
# must have tried at the exact same time.
nb_error += 1
if nb_error < 10:
continue
......@@ -252,19 +262,22 @@ def lock(tmp_dir, timeout=120, min_wait=5, max_wait=10, verbosity=1):
time.sleep(random.uniform(min_wait, max_wait))
continue
def refresh_lock(lock_file):
"""
'Refresh' an existing lock by re-writing the file containing the owner's
unique id, using a new (randomly generated) id, which is also returned.
"""
unique_id = '%s_%s_%s' % (os.getpid(),
''.join([str(random.randint(0,9)) for i in range(10)]),
socket.gethostname())
unique_id = '%s_%s_%s' % (
os.getpid(),
''.join([str(random.randint(0, 9)) for i in range(10)]),
socket.gethostname())
lock_write = open(lock_file, 'w')
lock_write.write(unique_id + '\n')
lock_write.close()
return unique_id
class Unlocker(object):
"""
Class wrapper around release mechanism so that the lock is automatically
......@@ -282,12 +295,14 @@ class Unlocker(object):
self.unlock()
def unlock(self):
"""
Remove current lock.
This function does not crash if it is unable to properly delete the lock
file and directory. The reason is that it should be allowed for multiple
jobs running in parallel to unlock the same directory at the same time
(e.g. when reaching their timeout limit).
"""Remove current lock.
This function does not crash if it is unable to properly
delete the lock file and directory. The reason is that it
should be allowed for multiple jobs running in parallel to
unlock the same directory at the same time (e.g. when reaching
their timeout limit).
"""
# If any error occurs, we assume this is because someone else tried to
# unlock this directory at the same time.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论