@@ -813,34 +813,33 @@ def __init__(self, *args):
813813 self .partial_result_ready , Qt .QueuedConnection )
814814
815815 @property
816- def future (self ):
817- # type: () -> Future
816+ def future (self ) -> Future :
818817 return self .__future
819818
820- def set_status (self , text ):
819+ def set_status (self , text : str ):
821820 self ._p_status_changed .emit (text )
822821
823- def set_progress_value (self , value ):
822+ def set_progress_value (self , value : float ):
824823 if round (value , 1 ) > round (self .__progress , 1 ):
825824 # Only emit progress when it has changed sufficiently
826825 self ._p_progress_changed .emit (value )
827826 self .__progress = value
828827
829- def set_partial_results (self , value ):
828+ def set_partial_result (self , value : Any ):
830829 self ._p_partial_result_ready .emit (value )
831830
832- def is_interruption_requested (self ):
831+ def is_interruption_requested (self ) -> bool :
833832 return self .__interruption_requested
834833
835- def start (self , executor , func = None ):
836- # type: (concurrent.futures.Executor, Callable[[], Any]) -> Future
834+ def start (self , executor : concurrent . futures . Executor ,
835+ func : Callable [[], Any ] = None ) -> Future :
837836 assert self .future is None
838837 assert not self .__interruption_requested
839838 self .__future = executor .submit (func )
840839 self .watcher .setFuture (self .future )
841840 return self .future
842841
843- def cancel (self ):
842+ def cancel (self ) -> bool :
844843 assert not self .__interruption_requested
845844 self .__interruption_requested = True
846845 if self .future is not None :
@@ -861,22 +860,55 @@ def __init__(self):
861860 self .__task = None # type: Optional[TaskState]
862861
863862 @property
864- def task (self ):
863+ def task (self ) -> TaskState :
865864 return self .__task
866865
867- def _prepare_task (self , state : TaskState ) -> Callable [[], Any ]:
866+ def on_partial_result (self , result : Any ) -> None :
867+ """ Invoked from runner (by state) to send the partial results
868+ The method should handle partial results, i.e. show them in the plot.
869+
870+ :param result: any data structure to hold temporary result
871+ """
868872 raise NotImplementedError
869873
870- def _set_partial_results (self , result : Any ) -> None :
874+ def on_done (self , result : Any ) -> None :
875+ """ Invoked when task is done.
876+ The method should re-set the result (to double check it) and
877+ perform operations with obtained results, eg. send data to the output.
878+
879+ :param result: any data structure to hold temporary result
880+ """
871881 raise NotImplementedError
872882
873- def _set_results (self , results : Any ) -> None :
874- # NOTE: All of these have already been set by _set_partial_results,
875- # we double check that they are aliases
876- for key in results .__dict__ :
877- value = getattr (results , key )
878- if value is not None :
879- setattr (self , key , value )
883+ def start (self , task : Callable , * args , ** kwargs ):
884+ """ Call from derived class to start the task.
885+ :param task: runner - a method to run in a thread - should accept
886+ `state` parameter
887+ """
888+ self .__cancel_task (wait = False )
889+
890+ if self .data is None :
891+ self .__set_state_ready ()
892+ return
893+
894+ assert callable (task ), "`task` must be callable!"
895+ state = TaskState (self )
896+ task = partial (task , * (args + (state ,)), ** kwargs )
897+
898+ self .__set_state_busy ()
899+ self .__start_task (task , state )
900+
901+ def cancel (self ):
902+ """ Call from derived class to stop the task. """
903+ self .__cancel_task (wait = False )
904+ self .__set_state_ready ()
905+
906+ def shutdown (self ):
907+ """ Call from derived class when the widget is deleted
908+ (in onDeleteWidget).
909+ """
910+ self .__cancel_task (wait = True )
911+ self .__executor .shutdown (True )
880912
881913 def __set_state_ready (self ):
882914 self .progressBarFinished ()
@@ -891,42 +923,28 @@ def __start_task(self, task: Callable[[], Any], state: TaskState):
891923 assert self .__task is None
892924 state .status_changed .connect (self .setStatusMessage )
893925 state .progress_changed .connect (self .progressBarSet )
894- state .partial_result_ready .connect (self ._set_partial_results )
895- state .watcher .done .connect (self .on_done )
926+ state .partial_result_ready .connect (self .on_partial_result )
927+ state .watcher .done .connect (self .__on_task_done )
896928 state .start (self .__executor , task )
897929 state .setParent (self )
898930 self .__task = state
899931
900- def __cancel_task (self , wait = True ):
932+ def __cancel_task (self , wait : bool = True ):
901933 if self .__task is not None :
902934 state , self .__task = self .__task , None
903935 state .cancel ()
904- state .partial_result_ready .disconnect (self ._set_partial_results )
936+ state .partial_result_ready .disconnect (self .on_partial_result )
905937 state .status_changed .disconnect (self .setStatusMessage )
906938 state .progress_changed .disconnect (self .progressBarSet )
907- state .watcher .done .disconnect (self .on_done )
939+ state .watcher .done .disconnect (self .__on_task_done )
908940 if wait :
909941 concurrent .futures .wait ([state .future ])
910942 state .deleteLater ()
911943 else :
912944 w = FutureWatcher (state .future , parent = state )
913945 w .done .connect (state .deleteLater )
914946
915- def start (self ):
916- """ Call to start the task. """
917- self .__cancel_task (wait = False )
918-
919- if self .data is None :
920- self .__set_state_ready ()
921- return
922-
923- state = TaskState (self )
924- task = self ._prepare_task (state )
925- self .__set_state_busy ()
926- self .__start_task (task , state )
927-
928- def on_done (self , future : Future ):
929- """ Invoked when task is done. """
947+ def __on_task_done (self , future : Future ):
930948 assert future .done ()
931949 assert self .__task is not None
932950 assert self .__task .future is future
@@ -935,14 +953,4 @@ def on_done(self, future: Future):
935953 task .deleteLater ()
936954 self .__set_state_ready ()
937955 result = future .result ()
938- self ._set_results (result )
939-
940- def cancel (self ):
941- """ Call to stop the task. """
942- self .__cancel_task (wait = False )
943- self .__set_state_ready ()
944-
945- def shutdown (self ):
946- """ Call when widget is deleted (in onDeleteWidget). """
947- self .__cancel_task (wait = True )
948- self .__executor .shutdown (True )
956+ self .on_done (result )
0 commit comments