88
99
1010class Task :
11- """ Abstraction for ML task. Should have interface similat to concurrent.futures.Future
11+ """Abstraction for ML task. Should have interface similat to concurrent.futures.Future
1212
13- Attributes:
14- db (Redis): database object
15- redis_key (RedisKey): redis keys associated with task
16- dataframe (DataFrame): task result
17- exception (Exception): task exeuton runtime exception
18- _timeout (int): max time without status updating
13+ Attributes:
14+ db (Redis): database object
15+ redis_key (RedisKey): redis keys associated with task
16+ dataframe (DataFrame): task result
17+ exception (Exception): task exeuton runtime exception
18+ _timeout (int): max time without status updating
1919 """
2020
2121 def __init__ (self , connection : redis .Redis , redis_key : RedisKey ) -> None :
2222 self .db = connection
2323 self .redis_key = redis_key
2424 self .dataframe = None
2525 self .exception = None
26- self ._timeout = 30
26+ self ._timeout = 60
2727
2828 def subscribe (self ) -> ML_TASK_STATUS :
29- """ return tasks status untill it is not done or failed
30- """
29+ """return tasks status untill it is not done or failed"""
3130 pubsub = self .db .pubsub ()
3231 cache = self .db .cache ()
3332 pubsub .subscribe (self .redis_key .status )
34- while ( msg := pubsub .get_message (timeout = self ._timeout ) ):
35- if msg [' type' ] not in pubsub .PUBLISH_MESSAGE_TYPES :
33+ while msg := pubsub .get_message (timeout = self ._timeout ):
34+ if msg [" type" ] not in pubsub .PUBLISH_MESSAGE_TYPES :
3635 continue
37- ml_task_status = ML_TASK_STATUS (msg [' data' ])
36+ ml_task_status = ML_TASK_STATUS (msg [" data" ])
3837 if ml_task_status == ML_TASK_STATUS .COMPLETE :
3938 dataframe_bytes = cache .get (self .redis_key .dataframe )
4039 if dataframe_bytes is not None :
@@ -51,32 +50,30 @@ def subscribe(self) -> ML_TASK_STATUS:
5150 yield ml_task_status
5251
5352 def wait (self , status : ML_TASK_STATUS = ML_TASK_STATUS .COMPLETE ) -> None :
54- """ block threasd untill task is not done or failed
55- """
53+ """block threasd untill task is not done or failed"""
5654 for status in self .subscribe ():
5755 if status in (ML_TASK_STATUS .WAITING , ML_TASK_STATUS .PROCESSING ):
5856 continue
5957 if status == ML_TASK_STATUS .ERROR :
6058 if self .exception is not None :
6159 raise self .exception
6260 else :
63- raise Exception (' Unknown error during ML task execution' )
61+ raise Exception (" Unknown error during ML task execution" )
6462 if status == ML_TASK_STATUS .TIMEOUT :
6563 raise Exception (f"Can't get answer in { self ._timeout } seconds" )
6664 if status == ML_TASK_STATUS .COMPLETE :
6765 return
68- raise KeyError (' Unknown task status' )
66+ raise KeyError (" Unknown task status" )
6967
7068 def result (self ) -> DataFrame :
71- """ wait task is done and return result
69+ """wait task is done and return result
7270
73- Returns:
74- DataFrame: task result
71+ Returns:
72+ DataFrame: task result
7573 """
7674 self .wait ()
7775 return self .dataframe
7876
7977 def add_done_callback (self , fn : Callable ) -> None :
80- """ need for compatability with concurrent.futures.Future interface
81- """
78+ """need for compatability with concurrent.futures.Future interface"""
8279 pass
0 commit comments