Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
2fe02fb0
提交
2fe02fb0
authored
10月 03, 2013
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pep8
上级
8afdfa27
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
37 行增加
和
22 行删除
+37
-22
compilelock.py
theano/gof/compilelock.py
+37
-22
没有找到文件。
theano/gof/compilelock.py
浏览文件 @
2fe02fb0
# 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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论