Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
experiment-control
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository 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
whuie2
experiment-control
Commits
4805b98a
Commit
4805b98a
authored
3 years ago
by
whooie
Browse files
Options
Downloads
Patches
Plain Diff
add thread termination upon receipt of a keyboard interrupt
parent
e0aa5f23
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/control.py
+72
-7
72 additions, 7 deletions
lib/control.py
with
72 additions
and
7 deletions
lib/control.py
+
72
−
7
View file @
4805b98a
from
__future__
import
annotations
import
threading
import
inspect
import
ctypes
import
sys
import
time
def
_async_raise
(
tid
,
exctype
):
"""
Raises the exception, performs cleanup if needed.
"""
if
not
inspect
.
isclass
(
exctype
):
raise
TypeError
(
"
Only types can be raised (not instances)
"
)
res
=
ctypes
.
pythonapi
.
PyThreadState_SetAsyncExc
(
ctypes
.
c_long
(
tid
),
ctypes
.
py_object
(
exctype
))
if
res
==
0
:
raise
ValueError
(
"
invalid thread id
"
)
elif
res
!=
1
:
# if it returns a number greater than one, you're in trouble,
# and you should call it again with exc=NULL to revert the effect"""
ctypes
.
pythonapi
.
PyThreadState_SetAsyncExc
(
tid
,
0
)
raise
SystemError
(
"
PyThreadState_SetAsyncExc failed
"
)
class
TerminableThread
(
threading
.
Thread
):
def
_get_my_tid
(
self
):
"""
Determines this (self
'
s) thread id.
"""
if
not
self
.
is_alive
():
raise
threading
.
ThreadError
(
"
the thread is not active
"
)
# do we have it cached?
if
hasattr
(
self
,
"
_thread_id
"
):
return
self
.
_thread_id
# no, look for it in the _active dict
for
tid
,
tobj
in
threading
.
_active
.
items
():
if
tobj
is
self
:
self
.
_thread_id
=
tid
return
tid
raise
AssertionError
(
"
could not determine the thread
'
s id
"
)
def
raise_exc
(
self
,
exctype
):
"""
Raises the given exception type in the context of this thread.
"""
_async_raise
(
self
.
_get_my_tid
(),
exctype
)
def
terminate
(
self
):
"""
Raises SystemExit in the context of the given thread, which should
cause the thread to exit silently (unless caught).
"""
self
.
raise_exc
(
SystemExit
)
class
Controller
:
def
precmd
(
self
,
*
args
):
raise
NotImplementedError
...
...
@@ -51,19 +103,32 @@ class Controller:
except
NotImplementedError
:
print
(
"
[control] no pre-command defined
"
)
threads
=
[
(
name
,
threading
.
Thread
(
target
=
fn
,
args
=
args
))
(
name
,
Terminable
Thread
(
target
=
fn
,
args
=
args
))
for
name
,
fn
in
actions
.
items
()
]
for
name
,
thread
in
threads
:
print
(
f
"
[control] action `
{
name
}
` started
"
)
thread
.
start
()
while
len
(
threads
)
>
0
:
try
:
while
len
(
threads
)
>
0
:
for
name
,
thread
in
threads
:
if
not
thread
.
is_alive
():
thread
.
join
()
print
(
f
"
[control] action `
{
name
}
` finished
"
)
threads
.
remove
((
name
,
thread
))
time
.
sleep
(
1
)
except
KeyboardInterrupt
:
print
(
"
[control] received keyboard interrupt; terminating all actions...
"
)
for
name
,
thread
in
threads
:
if
not
thread
.
is_alive
():
thread
.
join
()
print
(
f
"
[control] action `
{
name
}
` finished
"
)
threads
.
remove
((
name
,
thread
))
time
.
sleep
(
1
)
thread
.
terminate
()
while
len
(
threads
)
>
0
:
for
name
,
thread
in
threads
:
if
not
thread
.
is_alive
():
thread
.
join
()
print
(
f
"
[control] action `
{
name
}
` terminated
"
)
threads
.
remove
((
name
,
thread
))
time
.
sleep
(
1
)
return
None
try
:
self
.
postcmd
(
*
args
)
except
NotImplementedError
:
...
...
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