Skip to content

Commit a2451ff

Browse files
authored
fix/maxsize (#59)
1 parent 5909376 commit a2451ff

38 files changed

+171
-63
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [0.4.6] - 2020-10-11
4+
* Introduces the `maxsize` as an argument to `to_stage` and `to_iterable`.
5+
* `ordered` now takes an optinal `maxsize` parameter.
6+
37
## [0.4.5] - 2020-10-04
48
* Fixed `pl.task.from_iterable` to solve #56
59
* `pl.*.ordered` implementations now based on `bisect.insort`.

docs/index.md

+15
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@ data = (
167167
)
168168
```
169169

170+
## Run Tests
171+
A sample script is provided to run the tests in a container (either Docker or Podman is supported), to run tests:
172+
173+
```bash
174+
$ bash scripts/run-tests.sh
175+
```
176+
177+
This script can also receive a python version to check test against, i.e
178+
179+
```bash
180+
$ bash scripts/run-tests.sh 3.7
181+
```
182+
183+
170184
## Related Stuff
171185
* [Making an Unlimited Number of Requests with Python aiohttp + pypeln](https://medium.com/@cgarciae/making-an-infinite-number-of-requests-with-python-aiohttp-pypeln-3a552b97dc95)
172186
* [Process Pools](https://docs.python.org/3.4/library/multiprocessing.html?highlight=process#module-multiprocessing.pool)
@@ -177,6 +191,7 @@ data = (
177191

178192
## Contributors
179193
* [cgarciae](https://github.com/cgarciae)
194+
* [Davidnet](https://github.com/Davidnet)
180195

181196
## License
182197
MIT

poetry.lock

+49-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pypeln/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
from .utils import BaseStage
99

1010

11-
__version__ = "0.4.5"
11+
__version__ = "0.4.6"

pypeln/process/api/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def concat(
3939
A stage object.
4040
"""
4141

42-
dependencies = [to_stage(stage) for stage in stages]
42+
dependencies = [to_stage(stage, maxsize=maxsize) for stage in stages]
4343

4444
return Stage(
4545
process_fn=Concat(),

pypeln/process/api/each.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def process_image(image_path):
120120
)
121121
)
122122

123-
stage = to_stage(stage)
123+
stage = to_stage(stage, maxsize=maxsize)
124124

125125
stage = Stage(
126126
process_fn=Each(f),

pypeln/process/api/filter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def slow_gt3(x):
111111
)
112112
)
113113

114-
stage = to_stage(stage)
114+
stage = to_stage(stage, maxsize=maxsize)
115115

116116
return Stage(
117117
process_fn=Filter(f),

pypeln/process/api/flat_map.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def slow_integer_pair(x):
128128
)
129129
)
130130

131-
stage = to_stage(stage)
131+
stage = to_stage(stage, maxsize=maxsize)
132132

133133
return Stage(
134134
process_fn=FlatMap(f),

pypeln/process/api/from_iterable.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010

1111
class FromIterable(tp.NamedTuple):
1212
iterable: tp.Iterable
13+
maxsize: int
1314

1415
def __call__(self, worker: Worker, **kwargs):
1516

1617
iterable = self.iterable
1718

1819
if isinstance(iterable, pypeln_utils.BaseStage):
1920

20-
for x in iterable.to_iterable(maxsize=0, return_index=True):
21+
for x in iterable.to_iterable(maxsize=self.maxsize, return_index=True):
2122
worker.stage_params.output_queues.put(x)
2223
else:
2324
for i, x in enumerate(iterable):
@@ -30,18 +31,23 @@ def __call__(self, worker: Worker, **kwargs):
3031

3132

3233
@tp.overload
33-
def from_iterable(iterable: tp.Iterable[T], use_thread: bool = True) -> Stage[T]:
34+
def from_iterable(
35+
iterable: tp.Iterable[T], use_thread: bool = True, maxsize: int = 0
36+
) -> Stage[T]:
3437
...
3538

3639

3740
@tp.overload
38-
def from_iterable(use_thread: bool = True) -> pypeln_utils.Partial[Stage[T]]:
41+
def from_iterable(
42+
use_thread: bool = True, maxsize: int = 0
43+
) -> pypeln_utils.Partial[Stage[T]]:
3944
...
4045

4146

4247
def from_iterable(
4348
iterable: tp.Union[tp.Iterable[T], pypeln_utils.Undefined] = pypeln_utils.UNDEFINED,
4449
use_thread: bool = True,
50+
maxsize: int = 0,
4551
) -> tp.Union[Stage[T], pypeln_utils.Partial[Stage[T]]]:
4652
"""
4753
Creates a stage from an iterable.
@@ -60,9 +66,9 @@ def from_iterable(
6066
)
6167

6268
return Stage(
63-
process_fn=FromIterable(iterable),
69+
process_fn=FromIterable(iterable, maxsize=maxsize),
6470
workers=1,
65-
maxsize=0,
71+
maxsize=maxsize,
6672
timeout=0,
6773
total_sources=1,
6874
dependencies=[],

pypeln/process/api/map.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def slow_add1(x):
111111
)
112112
)
113113

114-
stage = to_stage(stage)
114+
stage = to_stage(stage, maxsize=maxsize)
115115

116116
return Stage(
117117
process_fn=Map(f),

pypeln/process/api/ordered.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@ def __call__(self, worker: Worker, **kwargs):
2222

2323

2424
@tp.overload
25-
def ordered(stage: Stage[A]) -> Stage[A]:
25+
def ordered(stage: Stage[A], maxsize: int = 0,) -> Stage[A]:
2626
...
2727

2828

2929
@tp.overload
30-
def ordered() -> pypeln_utils.Partial[Stage[A]]:
30+
def ordered(maxsize: int = 0) -> pypeln_utils.Partial[Stage[A]]:
3131
...
3232

3333

3434
def ordered(
3535
stage: tp.Union[
3636
Stage[A], tp.Iterable[A], pypeln_utils.Undefined
3737
] = pypeln_utils.UNDEFINED,
38+
maxsize: int = 0,
3839
) -> tp.Union[Stage[A], pypeln_utils.Partial[Stage[A]]]:
3940
"""
4041
Creates a stage that sorts its elements based on their order of creation on the source iterable(s) of the pipeline.
@@ -64,6 +65,7 @@ def slow_squared(x):
6465
6566
Arguments:
6667
stage: A Stage or Iterable.
68+
maxsize: The maximum number of objects the stage can hold simultaneously, if set to `0` (default) then the stage can grow unbounded.
6769
6870
Returns:
6971
If the `stage` parameters is given then this function returns an iterable, else it returns a `Partial`.
@@ -72,12 +74,12 @@ def slow_squared(x):
7274
if isinstance(stage, pypeln_utils.Undefined):
7375
return pypeln_utils.Partial(lambda stage: ordered(stage))
7476

75-
stage = to_stage(stage)
77+
stage = to_stage(stage, maxsize=maxsize)
7678

7779
return Stage(
7880
process_fn=Ordered(),
7981
workers=1,
80-
maxsize=0,
82+
maxsize=maxsize,
8183
timeout=0,
8284
total_sources=stage.workers,
8385
dependencies=[stage],

pypeln/process/api/to_stage.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from .from_iterable import from_iterable
77

88

9-
def to_stage(obj: tp.Union[Stage[A], tp.Iterable[A]]) -> Stage[A]:
9+
def to_stage(obj: tp.Union[Stage[A], tp.Iterable[A]], maxsize: int) -> Stage[A]:
1010

1111
if isinstance(obj, Stage):
1212
return obj
1313
else:
14-
return from_iterable(obj)
14+
return from_iterable(obj, maxsize=maxsize)

pypeln/sync/api/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def concat(
3535
A stage object.
3636
"""
3737

38-
dependencies = [to_stage(stage) for stage in stages]
38+
dependencies = [to_stage(stage, maxsize=maxsize) for stage in stages]
3939

4040
return Stage(
4141
process_fn=Concat(),

pypeln/sync/api/each.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def process_image(image_path):
123123
)
124124
)
125125

126-
stage_ = to_stage(stage)
126+
stage_ = to_stage(stage, maxsize=maxsize)
127127

128128
stage_ = Stage(
129129
process_fn=Each(f),

pypeln/sync/api/filter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def slow_gt3(x):
107107
)
108108
)
109109

110-
stage_ = to_stage(stage)
110+
stage_ = to_stage(stage, maxsize=maxsize)
111111

112112
return Stage(
113113
process_fn=Filter(f),

pypeln/sync/api/flat_map.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def slow_integer_pair(x):
123123
)
124124
)
125125

126-
stage_ = to_stage(stage)
126+
stage_ = to_stage(stage, maxsize=maxsize)
127127

128128
return Stage(
129129
process_fn=FlatMap(f),

pypeln/sync/api/from_iterable.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@
1010
@dataclass
1111
class FromIterable(ProcessFn):
1212
iterable: tp.Iterable
13+
maxsize: int
1314

1415
def __call__(self, worker: Stage, **kwargs) -> tp.Iterable:
1516
if isinstance(self.iterable, pypeln_utils.BaseStage):
16-
yield from self.iterable.to_iterable(maxsize=0, return_index=True)
17+
yield from self.iterable.to_iterable(
18+
maxsize=self.maxsize, return_index=True
19+
)
1720
else:
1821
for i, x in enumerate(self.iterable):
1922
if isinstance(x, pypeln_utils.Element):
@@ -23,18 +26,23 @@ def __call__(self, worker: Stage, **kwargs) -> tp.Iterable:
2326

2427

2528
@tp.overload
26-
def from_iterable(iterable: tp.Iterable[T], use_thread: bool = True) -> Stage[T]:
29+
def from_iterable(
30+
iterable: tp.Iterable[T], use_thread: bool = True, maxsize: int = 0
31+
) -> Stage[T]:
2732
...
2833

2934

3035
@tp.overload
31-
def from_iterable(use_thread: bool = True) -> pypeln_utils.Partial[Stage[T]]:
36+
def from_iterable(
37+
use_thread: bool = True, maxsize: int = 0
38+
) -> pypeln_utils.Partial[Stage[T]]:
3239
...
3340

3441

3542
def from_iterable(
3643
iterable: tp.Union[tp.Iterable[T], pypeln_utils.Undefined] = pypeln_utils.UNDEFINED,
3744
use_thread: bool = True,
45+
maxsize: int = 0,
3846
) -> tp.Union[Stage[T], pypeln_utils.Partial[Stage[T]]]:
3947
"""
4048
Creates a stage from an iterable.
@@ -51,7 +59,7 @@ def from_iterable(
5159
return pypeln_utils.Partial(lambda iterable: from_iterable(iterable))
5260

5361
return Stage(
54-
process_fn=FromIterable(iterable),
62+
process_fn=FromIterable(iterable, maxsize=maxsize),
5563
timeout=0,
5664
dependencies=[],
5765
on_start=None,

pypeln/sync/api/map.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def slow_add1(x):
109109
)
110110
)
111111

112-
stage_ = to_stage(stage)
112+
stage_ = to_stage(stage, maxsize=maxsize)
113113

114114
return Stage(
115115
process_fn=Map(f),

0 commit comments

Comments
 (0)