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
dff252da
Commit
dff252da
authored
4 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
add done callback
parent
202fefd1
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
satella/coding/concurrent/futures/collection.py
+16
-0
16 additions, 0 deletions
satella/coding/concurrent/futures/collection.py
tests/test_coding/test_concurrent.py
+26
-0
26 additions, 0 deletions
tests/test_coding/test_concurrent.py
with
42 additions
and
0 deletions
satella/coding/concurrent/futures/collection.py
+
16
−
0
View file @
dff252da
...
@@ -32,6 +32,22 @@ class FutureCollection:
...
@@ -32,6 +32,22 @@ class FutureCollection:
fc
.
append
(
other
)
fc
.
append
(
other
)
return
FutureCollection
(
fc
)
return
FutureCollection
(
fc
)
def
add_done_callback
(
self
,
callback
,
only_one
:
bool
=
False
)
->
None
:
"""
Add a callback to a Future to be called on it
'
s completion.
By default, this will add the callback to all futures.
:param callback: callback that takes the completed Future as argument
:param only_one: callback will be added only to a single Future. False by default
:raises IndexError: only_one was given and no Futures in collection!
"""
if
only_one
:
self
.
futures
[
0
].
add_done_callback
(
callback
)
else
:
for
future
in
self
.
futures
:
future
.
add_done_callback
(
callback
)
def
set_running_or_notify_cancel
(
self
)
->
bool
:
def
set_running_or_notify_cancel
(
self
)
->
bool
:
"""
"""
Call :code:`set_running_or_notify_cancel` on the futures
Call :code:`set_running_or_notify_cancel` on the futures
...
...
This diff is collapsed.
Click to expand it.
tests/test_coding/test_concurrent.py
+
26
−
0
View file @
dff252da
...
@@ -19,6 +19,32 @@ from satella.exceptions import WouldWaitMore, AlreadyAllocated, Empty
...
@@ -19,6 +19,32 @@ from satella.exceptions import WouldWaitMore, AlreadyAllocated, Empty
class
TestConcurrent
(
unittest
.
TestCase
):
class
TestConcurrent
(
unittest
.
TestCase
):
def
test_future_collection_callbacks_one
(
self
):
a
=
{
'
count
'
:
0
}
def
callback
(
fut
):
nonlocal
a
a
[
'
count
'
]
+=
1
fc
=
FutureCollection
([
PythonFuture
(),
PythonFuture
()])
fc
.
add_done_callback
(
callback
,
True
)
fc
.
set_running_or_notify_cancel
()
fc
.
set_result
(
None
)
self
.
assertEqual
(
a
[
'
count
'
],
1
)
def
test_future_collection_callbacks
(
self
):
a
=
{
'
count
'
:
0
}
def
callback
(
fut
):
nonlocal
a
a
[
'
count
'
]
+=
1
fc
=
FutureCollection
([
PythonFuture
(),
PythonFuture
()])
fc
.
add_done_callback
(
callback
)
fc
.
set_running_or_notify_cancel
()
fc
.
set_result
(
None
)
self
.
assertEqual
(
a
[
'
count
'
],
2
)
def
test_future_collection_cancel
(
self
):
def
test_future_collection_cancel
(
self
):
fc
=
FutureCollection
([
PythonFuture
(),
PythonFuture
()])
fc
=
FutureCollection
([
PythonFuture
(),
PythonFuture
()])
self
.
assertTrue
(
fc
.
set_running_or_notify_cancel
())
self
.
assertTrue
(
fc
.
set_running_or_notify_cancel
())
...
...
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