Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
satella
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container 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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
public
satella
Commits
97c4dfe2
Commit
97c4dfe2
authored
4 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
fixed thread
parent
c7932f10
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
satella/coding/concurrent/thread.py
+12
-1
12 additions, 1 deletion
satella/coding/concurrent/thread.py
tests/test_coding/test_concurrent.py
+5
-0
5 additions, 0 deletions
tests/test_coding/test_concurrent.py
with
17 additions
and
1 deletion
satella/coding/concurrent/thread.py
+
12
−
1
View file @
97c4dfe2
import
concurrent
import
ctypes
import
platform
import
threading
...
...
@@ -189,6 +188,10 @@ class TerminableThread(threading.Thread):
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
"""
Note that this is called in the constructor
'
s thread. Use .prepare() to
run statements that should be ran in new thread.
"""
super
().
__init__
(
*
args
,
**
kwargs
)
self
.
_terminating
=
False
# type: bool
...
...
@@ -197,6 +200,13 @@ class TerminableThread(threading.Thread):
"""
Return whether a termination of this thread was requested
"""
return
self
.
_terminating
def
prepare
(
self
)
->
None
:
"""
This is called before the .loop() looping loop is entered.
This is invoked already in a separate thread.
"""
def
loop
(
self
)
->
None
:
"""
Run one iteration of the loop. Meant to be overrided. You do not need to override it
...
...
@@ -218,6 +228,7 @@ class TerminableThread(threading.Thread):
"""
Calls self.loop() indefinitely, until terminating condition is met
"""
self
.
prepare
()
while
not
self
.
_terminating
:
self
.
loop
()
self
.
cleanup
()
...
...
This diff is collapsed.
Click to expand it.
tests/test_coding/test_concurrent.py
+
5
−
0
View file @
97c4dfe2
...
...
@@ -260,7 +260,11 @@ class TestConcurrent(unittest.TestCase):
def
test_terminable_thread
(
self
):
class
MyTerminableThread
(
TerminableThread
):
def
prepare
(
self
):
self
.
a
=
5
def
loop
(
self
):
self
.
a
+=
1
time
.
sleep
(
0.5
)
mtt
=
MyTerminableThread
()
...
...
@@ -270,6 +274,7 @@ class TestConcurrent(unittest.TestCase):
@unittest.skipIf
(
platform
.
python_implementation
()
!=
'
PyPy
'
,
'
this requires PyPy
'
)
def
test_terminable_thread_force_notimplementederror
(
self
):
class
MyTerminableThread
(
TerminableThread
):
def
run
(
self
):
a
=
0
while
not
self
.
terminating
:
...
...
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