Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
west-public
West
Commits
aaf2dcd7
Commit
aaf2dcd7
authored
Sep 29, 2019
by
Marco Govoni
Browse files
Added abstract methods and inheritance to facilitate client/server
parent
0c167185
Changes
1
Hide whitespace changes
Inline
Side-by-side
Pytools/lockfile.py
View file @
aaf2dcd7
...
...
@@ -2,100 +2,127 @@
from
time
import
sleep
,
perf_counter
as
pc
from
os
import
path
,
remove
from
abc
import
ABC
,
abstractmethod
def
server_input_file
(
client_lockfile
)
:
#
# Determine the name of the server file given the name of the lockfile
#
client_image
=
client_lockfile
.
split
(
"."
)[
1
]
return
f
"qb.
{
client_image
}
.in"
# we assume that server_number = client_image
##############
# SUPERCLASS #
##############
class
ClientServer
(
ABC
):
#
def
__init__
(
self
,
client_lockfile
,
maxsec
=
21600
,
sleepsec
=
10
):
#
self
.
client_lockfile
=
client_lockfile
self
.
maxsec
=
maxsec
self
.
sleepsec
=
sleepsec
super
().
__init__
()
#
@
abstractmethod
def
before_sleep
(
self
):
# subclass needs to implement this method
pass
#
@
abstractmethod
def
after_sleep
(
self
):
# subclass needs to implement this method
pass
#
@
abstractmethod
def
awake_condition
(
self
):
# subclass needs to implement this method
pass
#
def
start
(
self
):
#
# ====================================
self
.
before_sleep
()
# ====================================
#
awake
=
1
# I am awake if this is zero
t0
=
pc
()
while
(
pc
()
-
t0
<=
self
.
maxsec
)
:
#
# =================================
self
.
awake_condition
()
# =================================
#
exists
=
path
.
exists
(
self
.
client_lockfile
)
if
(
not
exists
)
:
awake
=
0
break
else
:
sleep
(
self
.
sleepsec
)
#
# ====================================
self
.
after_sleep
()
# ====================================
return
awake
def
create_input_file_for_server
(
client_lockfile
)
:
#
# List of perturbation files
#
perturbation_list
=
[]
with
open
(
client_lockfile
,
"r"
)
as
f
:
for
cnt
,
line
in
enumerate
(
f
):
perturbation_list
.
append
(
line
)
#
# Create the input file for the server
#
with
open
(
server_input_file
(
client_lockfile
),
"w"
)
as
f
:
f
.
write
(
"load gs.xml
\n
"
)
for
pert
in
perturbation_list
:
f
.
write
(
f
"response -vext filename
{
pert
}
"
)
###############
# SERVERCLASS #
###############
def
before_sleep
(
client_lockfile
)
:
#
create_input_file_for_server
(
client_lockfile
)
#
# Awake server, by removing its lockfile
#
if
(
path
.
exists
(
server_input_file
(
client_lockfile
)
+
".lock"
))
:
remove
(
server_input_file
(
client_lockfile
)
+
".lock"
)
def
awake_condition
(
client_lockfile
)
:
#
# If server gets to sleeps, awake the client
#
if
(
path
.
exists
(
server_input_file
(
client_lockfile
)
+
".lock"
))
:
remove
(
client_lockfile
)
class
QboxServer
(
ClientServer
)
:
#
def
before_sleep
(
self
):
#
# Determine the name of the server file
#
client_image
=
self
.
client_lockfile
.
split
(
"."
)[
1
]
self
.
server_inputfile
=
f
"qb.
{
client_image
}
.in"
# we assume that server_number = client_image
#
# List of perturbation files
#
perturbation_list
=
[]
with
open
(
self
.
client_lockfile
,
"r"
)
as
f
:
for
cnt
,
line
in
enumerate
(
f
):
perturbation_list
.
append
(
line
)
#
# Create the input file for the server
#
with
open
(
self
.
server_inputfile
,
"w"
)
as
f
:
f
.
write
(
"load gs.xml
\n
"
)
for
pert
in
perturbation_list
:
f
.
write
(
f
"response -vext filename
{
pert
}
"
)
#
# Awake server, by removing its lockfile
#
if
(
path
.
exists
(
self
.
server_inputfile
+
".lock"
))
:
remove
(
self
.
server_inputfile
+
".lock"
)
def
after_sleep
()
:
#
# HACK here
#
pass
#
def
awake_condition
(
self
):
#
# If server gets to sleeps, awake the client
#
if
(
path
.
exists
(
self
.
server_inputfile
+
".lock"
))
:
remove
(
self
.
client_lockfile
)
#
def
after_sleep
(
self
):
pass
#############
# INTERFACE #
#############
def
sleep_and_wait_for_lock_to_be_removed
(
*
args
,
**
kwargs
):
#
# Name of lockfile
client_lockfile
=
args
[
0
]
# name of client lockfile
maxsec
=
12
*
60
*
60
# 12 hours, Max sleep time (in s)
sleepsec
=
1
# 1 second, Sleep interval (in s)
#
client_lockfile
=
args
[
0
]
# change defaults
#
# Max sleep time (in s)
#
maxsec
=
12
*
60
*
60
# 12 hours
if
"maxsec"
in
kwargs
.
keys
()
:
maxsec
=
kwargs
[
"maxsec"
]
#
# Sleep interval (in s)
#
sleepsec
=
1
# 1 second
if
"sleepsec"
in
kwargs
.
keys
()
:
sleepsec
=
kwargs
[
"sleepsec"
]
#
# ====================================
before_sleep
(
client_lockfile
)
# ====================================
#
awake
=
1
# I am awake if this is zero
t0
=
pc
()
while
(
pc
()
-
t0
<=
maxsec
)
:
#
# =================================
awake_condition
(
client_lockfile
)
# =================================
#
exists
=
path
.
exists
(
client_lockfile
)
if
(
not
exists
)
:
awake
=
0
break
else
:
sleep
(
sleepsec
)
#
# ====================================
after_sleep
()
# ====================================
server
=
QboxServer
(
client_lockfile
,
maxsec
,
sleepsec
)
return_int
=
server
.
start
()
#
return
awake
return
return_int
########
# TEST #
########
def
test
()
:
with
open
(
"I.1.lock"
,
"w"
)
as
f
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment