@@ -864,6 +864,34 @@ def activate(self, suffix=''):
864
864
class BroadcastView (DirectView ):
865
865
is_coalescing = Bool (False )
866
866
867
+ def _init_metadata (self , s_idents ):
868
+ """initialize request metadata"""
869
+ return dict (
870
+ targets = s_idents ,
871
+ is_broadcast = True ,
872
+ is_coalescing = self .is_coalescing ,
873
+ )
874
+
875
+ def _make_async_result (self , message_future , s_idents , ** kwargs ):
876
+ original_msg_id = message_future .msg_id
877
+ if not self .is_coalescing :
878
+ futures = []
879
+ for ident in s_idents :
880
+ msg_and_target_id = f'{ original_msg_id } _{ ident } '
881
+ future = self .client .create_message_futures (
882
+ msg_and_target_id , async_result = True , track = True
883
+ )
884
+ self .client .outstanding .add (msg_and_target_id )
885
+ self .outstanding .add (msg_and_target_id )
886
+ futures .append (future [0 ])
887
+ if original_msg_id in self .outstanding :
888
+ self .outstanding .remove (original_msg_id )
889
+ else :
890
+ self .client .outstanding .add (original_msg_id )
891
+ futures = message_future
892
+
893
+ return AsyncResult (self .client , futures , owner = True , ** kwargs )
894
+
867
895
@sync_results
868
896
@save_ids
869
897
def _really_apply (
@@ -883,44 +911,62 @@ def _really_apply(
883
911
884
912
s_idents = [ident .decode ("utf8" ) for ident in idents ]
885
913
886
- metadata = dict (
887
- targets = s_idents , is_broadcast = True , is_coalescing = self .is_coalescing
914
+ metadata = self ._init_metadata (s_idents )
915
+ message_future = self .client .send_apply_request (
916
+ self ._socket , pf , pargs , pkwargs , track = track , metadata = metadata
917
+ )
918
+ ar = self ._make_async_result (
919
+ message_future , s_idents , fname = getname (f ), targets = targets
888
920
)
889
- if not self .is_coalescing :
890
- original_future = self .client .send_apply_request (
891
- self ._socket , pf , pargs , pkwargs , track = track , metadata = metadata
892
- )
893
- original_msg_id = original_future .msg_id
894
921
895
- for ident in s_idents :
896
- msg_and_target_id = f'{ original_msg_id } _{ ident } '
897
- future = self .client .create_message_futures (
898
- msg_and_target_id , async_result = True , track = True
899
- )
900
- self .client .outstanding .add (msg_and_target_id )
901
- self .outstanding .add (msg_and_target_id )
902
- futures .append (future [0 ])
903
- if original_msg_id in self .outstanding :
904
- self .outstanding .remove (original_msg_id )
905
- else :
906
- message_future = self .client .send_apply_request (
907
- self ._socket , pf , pargs , pkwargs , track = track , metadata = metadata
908
- )
909
- self .client .outstanding .add (message_future .msg_id )
910
- futures = message_future
922
+ if block :
923
+ try :
924
+ return ar .get ()
925
+ except KeyboardInterrupt :
926
+ pass
927
+ return ar
911
928
912
- ar = AsyncResult (
913
- self .client , futures , fname = getname (f ), targets = _targets , owner = True
929
+ @sync_results
930
+ @save_ids
931
+ def execute (self , code , silent = True , targets = None , block = None ):
932
+ """Executes `code` on `targets` in blocking or nonblocking manner.
933
+
934
+ ``execute`` is always `bound` (affects engine namespace)
935
+
936
+ Parameters
937
+ ----------
938
+ code : str
939
+ the code string to be executed
940
+ block : bool
941
+ whether or not to wait until done to return
942
+ default: self.block
943
+ """
944
+ block = self .block if block is None else block
945
+ targets = self .targets if targets is None else targets
946
+
947
+ _idents , _targets = self .client ._build_targets (targets )
948
+ s_idents = [ident .decode ("utf8" ) for ident in _idents ]
949
+
950
+ metadata = self ._init_metadata (s_idents )
951
+ message_future = self .client .send_execute_request (
952
+ self ._socket ,
953
+ code ,
954
+ silent = silent ,
955
+ metadata = metadata ,
956
+ )
957
+ ar = self ._make_async_result (
958
+ message_future , s_idents , fname = 'execute' , targets = _targets
914
959
)
915
960
if block :
916
961
try :
917
- return ar .get ()
962
+ ar .get ()
963
+ ar .wait_for_output ()
918
964
except KeyboardInterrupt :
919
965
pass
920
966
return ar
921
967
922
968
def map (self , f , * sequences , ** kwargs ):
923
- pass
969
+ raise NotImplementedError ( "BroadcastView.map not yet implemented" )
924
970
925
971
926
972
class LoadBalancedView (View ):
0 commit comments