@@ -98,14 +98,15 @@ class View(HasTraits):
9898 block = Bool (False )
9999 track = Bool (False )
100100 targets = Any ()
101+ label = Any ()
101102
102103 history = List ()
103104 outstanding = Set ()
104105 results = Dict ()
105106 client = Instance ('ipyparallel.Client' , allow_none = True )
106107
107108 _socket = Any ()
108- _flag_names = List (['targets' , 'block' , 'track' ])
109+ _flag_names = List (['targets' , 'block' , 'track' , 'label' ])
109110 _in_sync_results = Bool (False )
110111 _targets = Any ()
111112 _idents = Any ()
@@ -155,6 +156,8 @@ def set_flags(self, **kwargs):
155156 else :
156157 setattr (self , name , value )
157158
159+ return self # returning self would allow direct calling of map/apply in one command (no context manager)
160+
158161 @contextmanager
159162 def temp_flags (self , ** kwargs ):
160163 """temporarily set flags, for use in `with` statements.
@@ -530,7 +533,14 @@ def use_pickle(self):
530533 @sync_results
531534 @save_ids
532535 def _really_apply (
533- self , f , args = None , kwargs = None , targets = None , block = None , track = None
536+ self ,
537+ f ,
538+ args = None ,
539+ kwargs = None ,
540+ targets = None ,
541+ block = None ,
542+ track = None ,
543+ label = None ,
534544 ):
535545 """calls f(*args, **kwargs) on remote engines, returning the result.
536546
@@ -562,6 +572,8 @@ def _really_apply(
562572 block = self .block if block is None else block
563573 track = self .track if track is None else track
564574 targets = self .targets if targets is None else targets
575+ label = self .label if label is None else label
576+ metadata = dict (label = label )
565577
566578 _idents , _targets = self .client ._build_targets (targets )
567579 futures = []
@@ -572,7 +584,13 @@ def _really_apply(
572584
573585 for ident in _idents :
574586 future = self .client .send_apply_request (
575- self ._socket , pf , pargs , pkwargs , track = track , ident = ident
587+ self ._socket ,
588+ pf ,
589+ pargs ,
590+ pkwargs ,
591+ track = track ,
592+ ident = ident ,
593+ metadata = metadata ,
576594 )
577595 futures .append (future )
578596 if track :
@@ -592,7 +610,15 @@ def _really_apply(
592610 return ar
593611
594612 @sync_results
595- def map (self , f , * sequences , block = None , track = False , return_exceptions = False ):
613+ def map (
614+ self ,
615+ f ,
616+ * sequences ,
617+ block = None ,
618+ track = False ,
619+ return_exceptions = False ,
620+ label = None ,
621+ ):
596622 """Parallel version of builtin `map`, using this View's `targets`.
597623
598624 There will be one task per target, so work will be chunked
@@ -630,10 +656,17 @@ def map(self, f, *sequences, block=None, track=False, return_exceptions=False):
630656
631657 if block is None :
632658 block = self .block
659+ if label is None :
660+ label = self .label
633661
634662 assert len (sequences ) > 0 , "must have some sequences to map onto!"
635663 pf = ParallelFunction (
636- self , f , block = block , track = track , return_exceptions = return_exceptions
664+ self ,
665+ f ,
666+ block = block ,
667+ track = track ,
668+ return_exceptions = return_exceptions ,
669+ label = label ,
637670 )
638671 return pf .map (* sequences )
639672
@@ -1036,7 +1069,15 @@ def _broadcast_map(f, *sequence_names):
10361069 return list (map (f , * sequences ))
10371070
10381071 @_not_coalescing
1039- def map (self , f , * sequences , block = None , track = False , return_exceptions = False ):
1072+ def map (
1073+ self ,
1074+ f ,
1075+ * sequences ,
1076+ block = None ,
1077+ track = False ,
1078+ return_exceptions = False ,
1079+ label = None ,
1080+ ):
10401081 """Parallel version of builtin `map`, using this View's `targets`.
10411082
10421083 There will be one task per engine, so work will be chunked
@@ -1176,10 +1217,11 @@ class LoadBalancedView(View):
11761217 after = Any ()
11771218 timeout = CFloat ()
11781219 retries = Integer (0 )
1220+ label = Any ()
11791221
11801222 _task_scheme = Any ()
11811223 _flag_names = List (
1182- ['targets' , 'block' , 'track' , 'follow' , 'after' , 'timeout' , 'retries' ]
1224+ ['targets' , 'block' , 'track' , 'follow' , 'after' , 'timeout' , 'retries' , 'label' ]
11831225 )
11841226 _outstanding_maps = Set ()
11851227
@@ -1275,6 +1317,8 @@ def set_flags(self, **kwargs):
12751317
12761318 self .timeout = t
12771319
1320+ return self # returning self would allow direct calling of map/apply in one command (no context manager)
1321+
12781322 @sync_results
12791323 @save_ids
12801324 def _really_apply (
@@ -1289,6 +1333,7 @@ def _really_apply(
12891333 timeout = None ,
12901334 targets = None ,
12911335 retries = None ,
1336+ label = None ,
12921337 ):
12931338 """calls f(*args, **kwargs) on a remote engine, returning the result.
12941339
@@ -1344,6 +1389,7 @@ def _really_apply(
13441389 follow = self .follow if follow is None else follow
13451390 timeout = self .timeout if timeout is None else timeout
13461391 targets = self .targets if targets is None else targets
1392+ label = self .label if label is None else label
13471393
13481394 if not isinstance (retries , int ):
13491395 raise TypeError (f'retries must be int, not { type (retries )!r} ' )
@@ -1358,7 +1404,12 @@ def _really_apply(
13581404 after = self ._render_dependency (after )
13591405 follow = self ._render_dependency (follow )
13601406 metadata = dict (
1361- after = after , follow = follow , timeout = timeout , targets = idents , retries = retries
1407+ after = after ,
1408+ follow = follow ,
1409+ timeout = timeout ,
1410+ targets = idents ,
1411+ retries = retries ,
1412+ label = label ,
13621413 )
13631414
13641415 future = self .client .send_apply_request (
@@ -1389,6 +1440,7 @@ def map(
13891440 chunksize = 1 ,
13901441 ordered = True ,
13911442 return_exceptions = False ,
1443+ label = None ,
13921444 ):
13931445 """Parallel version of builtin `map`, load-balanced by this View.
13941446
@@ -1433,6 +1485,8 @@ def map(
14331485 # default
14341486 if block is None :
14351487 block = self .block
1488+ if label is None :
1489+ label = self .label
14361490
14371491 assert len (sequences ) > 0 , "must have some sequences to map onto!"
14381492
@@ -1443,6 +1497,7 @@ def map(
14431497 chunksize = chunksize ,
14441498 ordered = ordered ,
14451499 return_exceptions = return_exceptions ,
1500+ label = label ,
14461501 )
14471502 return pf .map (* sequences )
14481503
0 commit comments