Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Open sidebar
Nathan Graule
call
Commits
b747fb73
Commit
b747fb73
authored
Aug 07, 2018
by
Nathan Graule
💻
Browse files
Add Call.all() function
parent
319d33fe
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
0 deletions
+33
-0
call/__init__.py
call/__init__.py
+19
-0
tests/test_calls.py
tests/test_calls.py
+14
-0
No files found.
call/__init__.py
View file @
b747fb73
from
threading
import
Thread
from
typing
import
Callable
,
Any
,
TypeVar
,
Optional
from
typing
import
Iterable
__all__
=
[
'Call'
]
...
...
@@ -103,6 +104,24 @@ class Call:
error
=
Exception
(
error
)
return
Call
(
lambda
res
,
rej
:
rej
(
error
))
@
classmethod
def
all
(
cls
,
calls
):
# type: (Iterable[Call]) -> Call
"""Resolve a list of calls' resolved values, or fail with the first exception
:param calls: List of calls to resolve, in the same order than the Calls list"""
def
func
():
values
=
[]
try
:
for
call
in
calls
:
values
.
append
(
call
.
wait
())
except
Exception
:
raise
return
values
return
Call
.
from_function
(
func
)
@
classmethod
def
from_function
(
cls
,
func
,
*
args
,
**
kwargs
):
# type: (Callable[[Any], T], *Any, **Any) -> Call
...
...
tests/test_calls.py
View file @
b747fb73
...
...
@@ -2,6 +2,7 @@ from __future__ import print_function
import
json
import
os
import
random
import
time
import
unittest
...
...
@@ -84,3 +85,16 @@ class TestCall(unittest.TestCase):
self
.
assertNotEqual
(
value
,
'org.sindresorhus.Caprine'
)
self
.
assertTrue
(
isinstance
(
value
,
Exception
))
def
test_all
(
self
):
calls
=
[]
for
i
in
range
(
10
):
def
func
():
time
.
sleep
(
2
+
random
.
random
())
return
i
calls
.
append
(
Call
.
from_function
(
func
))
results
=
Call
.
all
(
calls
).
wait
()
for
i
in
range
(
10
):
self
.
assertEqual
(
results
[
i
],
i
)
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