Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pycalibration
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
calibration
pycalibration
Commits
09b48f21
Commit
09b48f21
authored
4 years ago
by
Thomas Kluyver
Browse files
Options
Downloads
Patches
Plain Diff
Use asyncio subprocess interface in webservice code
parent
233b8f7e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!435
Use asyncio subprocess interface in webservice code
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
webservice/webservice.py
+25
-20
25 additions, 20 deletions
webservice/webservice.py
with
25 additions
and
20 deletions
webservice/webservice.py
+
25
−
20
View file @
09b48f21
...
...
@@ -7,7 +7,6 @@ import json
import
logging
import
os
import
sqlite3
import
subprocess
# FIXME: use asyncio.create_subprocess_*
import
traceback
import
urllib.parse
from
asyncio
import
get_event_loop
,
shield
...
...
@@ -206,6 +205,16 @@ async def change_config(socket, config, updated_config, karabo_id, instrument,
socket
.
send
(
yaml
.
dump
(
new_conf
,
default_flow_style
=
False
).
encode
())
async
def
run_proc_async
(
cmd
:
List
[
str
])
->
(
int
,
bytes
):
"""
Run a subprocess to completion using asyncio, capturing stdout
Returns the numeric exit code and stdout (bytes)
"""
proc
=
await
asyncio
.
create_subprocess_exec
(
*
cmd
,
stdout
=
asyncio
.
subprocess
.
PIPE
)
stdout
,
_
=
proc
.
communicate
()
return
proc
.
returncode
,
stdout
async
def
slurm_status
(
filter_user
=
True
):
"""
Return the status of slurm jobs by calling squeue
...
...
@@ -216,9 +225,9 @@ async def slurm_status(filter_user=True):
cmd
=
[
"
squeue
"
]
if
filter_user
:
cmd
+=
[
"
-u
"
,
getpass
.
getuser
()]
ret
=
subprocess
.
run
(
cmd
,
stdout
=
subprocess
.
PIPE
)
# FIXME: asyncio
if
ret
.
return
code
==
0
:
rlines
=
ret
.
stdout
.
decode
().
split
(
"
\n
"
)
ret
code
,
stdout
=
run_proc_async
(
cmd
)
if
retcode
==
0
:
rlines
=
ret
code
.
decode
().
split
(
"
\n
"
)
statii
=
{}
for
r
in
rlines
[
1
:]:
try
:
...
...
@@ -239,9 +248,9 @@ async def slurm_job_status(jobid):
"""
cmd
=
[
"
sacct
"
,
"
-j
"
,
str
(
jobid
),
"
--format=JobID,Elapsed,state
"
]
ret
=
subprocess
.
run
(
cmd
,
stdout
=
subprocess
.
PIPE
)
# FIXME: asyncio
if
ret
.
return
code
==
0
:
rlines
=
ret
.
stdout
.
decode
().
split
(
"
\n
"
)
ret
code
,
stdout
=
run_proc_async
(
cmd
)
if
retcode
==
0
:
rlines
=
stdout
.
decode
().
split
(
"
\n
"
)
logging
.
debug
(
"
Job {} state {}
"
.
format
(
jobid
,
rlines
[
2
].
split
()))
if
len
(
rlines
[
2
].
split
())
==
3
:
...
...
@@ -422,15 +431,15 @@ async def run_action(job_db, cmd, mode, proposal, run, rid):
# FIXME: this coro has too many returns that can be simplified
if
mode
==
"
prod
"
:
logging
.
info
(
"
"
.
join
(
cmd
))
ret
=
subprocess
.
run
(
cmd
,
stdout
=
subprocess
.
PIPE
)
# FIXME: asyncio
if
ret
.
return
code
==
0
:
ret
code
,
stdout
=
run_proc_async
(
cmd
)
if
retcode
==
0
:
if
"
DARK
"
in
cmd
:
logging
.
info
(
Success
.
START_CHAR
.
format
(
proposal
,
run
))
else
:
logging
.
info
(
Success
.
START_CORRECTION
.
format
(
proposal
,
run
))
# enter jobs in job db
c
=
job_db
.
cursor
()
# FIXME: asyncio
rstr
=
ret
.
stdout
.
decode
()
rstr
=
stdout
.
decode
()
query
=
"
INSERT INTO jobs VALUES (
'
{rid}
'
,
'
{jobid}
'
,
'
{proposal}
'
,
'
{run}
'
,
'
PD
'
,
'
{now}
'
,
'
{det}
'
,
'
{act}
'
)
"
# noqa
for
r
in
rstr
.
split
(
"
\n
"
):
...
...
@@ -447,8 +456,8 @@ async def run_action(job_db, cmd, mode, proposal, run, rid):
else
:
return
Success
.
START_CORRECTION
.
format
(
proposal
,
run
)
else
:
logging
.
error
(
Errors
.
JOB_LAUNCH_FAILED
.
format
(
cmd
,
ret
.
return
code
))
return
Errors
.
JOB_LAUNCH_FAILED
.
format
(
cmd
,
ret
.
return
code
)
logging
.
error
(
Errors
.
JOB_LAUNCH_FAILED
.
format
(
cmd
,
retcode
))
return
Errors
.
JOB_LAUNCH_FAILED
.
format
(
cmd
,
retcode
)
else
:
if
"
DARK
"
in
cmd
:
...
...
@@ -476,8 +485,6 @@ async def wait_on_transfer(rpath, max_tries=300):
# check the copy is finished (ie. that the files are complete).
if
'
pnfs
'
in
os
.
path
.
realpath
(
rpath
):
return
True
rstr
=
None
ret
=
None
tries
=
0
# FIXME: if not kafka, then do event-driven, no sleep
...
...
@@ -490,16 +497,14 @@ async def wait_on_transfer(rpath, max_tries=300):
# FIXME: if not kafka, then do event-driven, no sleep
# wait until files are migrated
while
rstr
is
None
or
'
status=
"
online
"'
in
rstr
or
'
status=
"
Online
"'
in
rstr
or
ret
.
returncode
!=
0
:
# noqa
while
True
:
await
asyncio
.
sleep
(
10
)
# FIXME: make use of asyncio.subprocess.run
ret
=
subprocess
.
run
([
"
getfattr
"
,
"
-n
"
,
"
user.status
"
,
rpath
],
stdout
=
subprocess
.
PIPE
)
rstr
=
ret
.
stdout
.
decode
()
retcode
,
stdout
=
run_proc_async
([
"
getfattr
"
,
"
-n
"
,
"
user.status
"
,
rpath
])
if
retcode
==
0
and
'
status=
"
online
"'
not
in
stdout
.
decode
().
lower
():
return
True
if
tries
>
max_tries
:
return
False
tries
+=
1
return
ret
.
returncode
==
0
def
check_files
(
in_folder
:
str
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment