Skip to content

Commit fd71560

Browse files
author
Johannes Otepka
committed
corrections to set label using the standard flags feature
optional: returing self in View.set_flags allowing compact list comprehension for setting labels
1 parent 1b87bef commit fd71560

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

docs/source/examples/basic_task_label.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ def wait(t):
2121
# use load balanced view
2222
bview = rc.load_balanced_view()
2323
ar_list_b1 = [
24-
bview.map_async(wait, [2], label=f"mylabel_map_{i:02}") for i in range(10)
24+
bview.set_flags(label=f"mylabel_map_{i:02}").map_async(wait, [2]) for i in range(10)
2525
]
2626
ar_list_b2 = [
27-
bview.apply_async(wait, 2, label=f"mylabel_apply_{i:02}") for i in range(10)
27+
bview.set_flags(label=f"mylabel_map_{i:02}").apply_async(wait, 2) for i in range(10)
2828
]
2929
bview.wait(ar_list_b1)
3030
bview.wait(ar_list_b2)
@@ -33,10 +33,12 @@ def wait(t):
3333
# use direct view
3434
dview = rc[:]
3535
ar_list_d1 = [
36-
dview.apply_async(wait, 2, label=f"mylabel_map_{i + 10:02}") for i in range(10)
36+
dview.set_flags(label=f"mylabel_map_{i + 10:02}").apply_async(wait, 2)
37+
for i in range(10)
3738
]
3839
ar_list_d2 = [
39-
dview.map_async(wait, [2], label=f"mylabel_apply_{i + 10:02}") for i in range(10)
40+
dview.set_flags(label=f"mylabel_map_{i + 10:02}").map_async(wait, [2])
41+
for i in range(10)
4042
]
4143
dview.wait(ar_list_d1)
4244
dview.wait(ar_list_d2)

ipyparallel/client/view.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ def set_flags(self, **kwargs):
156156
else:
157157
setattr(self, name, value)
158158

159+
return self # returning self would allow direct calling of map/apply in one command (no context manager)
160+
159161
@contextmanager
160162
def temp_flags(self, **kwargs):
161163
"""temporarily set flags, for use in `with` statements.
@@ -570,12 +572,7 @@ def _really_apply(
570572
block = self.block if block is None else block
571573
track = self.track if track is None else track
572574
targets = self.targets if targets is None else targets
573-
label = (
574-
self.label if label is None else label
575-
) # comes into play when calling map[_async] (self.label)
576-
label = (
577-
kwargs.pop("label") if "label" in kwargs and label is None else label
578-
) # this is required can calling apply[_async]
575+
label = self.label if label is None else label
579576
metadata = dict(label=label)
580577

581578
_idents, _targets = self.client._build_targets(targets)
@@ -659,6 +656,8 @@ def map(
659656

660657
if block is None:
661658
block = self.block
659+
if label is None:
660+
label = self.label
662661

663662
assert len(sequences) > 0, "must have some sequences to map onto!"
664663
pf = ParallelFunction(
@@ -1318,6 +1317,8 @@ def set_flags(self, **kwargs):
13181317

13191318
self.timeout = t
13201319

1320+
return self # returning self would allow direct calling of map/apply in one command (no context manager)
1321+
13211322
@sync_results
13221323
@save_ids
13231324
def _really_apply(
@@ -1388,12 +1389,7 @@ def _really_apply(
13881389
follow = self.follow if follow is None else follow
13891390
timeout = self.timeout if timeout is None else timeout
13901391
targets = self.targets if targets is None else targets
1391-
label = (
1392-
self.label if label is None else label
1393-
) # comes into play when calling map[_async] (self.label)
1394-
label = (
1395-
kwargs.pop("label") if "label" in kwargs and label is None else label
1396-
) # this is required can calling apply[_async]
1392+
label = self.label if label is None else label
13971393

13981394
if not isinstance(retries, int):
13991395
raise TypeError(f'retries must be int, not {type(retries)!r}')
@@ -1489,6 +1485,8 @@ def map(
14891485
# default
14901486
if block is None:
14911487
block = self.block
1488+
if label is None:
1489+
label = self.label
14921490

14931491
assert len(sequences) > 0, "must have some sequences to map onto!"
14941492

0 commit comments

Comments
 (0)