@@ -50,7 +50,6 @@ def __init__(
5050 logger (Optional[Logger], optional): Custom logger to use. Defaults to None.
5151 """
5252 self .update_callback = None
53- self .parent_conn = None
5453 self .host = host
5554 self .port = port
5655 self .user = user
@@ -83,7 +82,7 @@ def _create_subscription_process(
8382 self ._cleanup_connections_and_processes ()
8483
8584 self .parent_conn , self .child_conn = Pipe ()
86- self .subscription_proces = Process (
85+ self .subscription_process = Process (
8786 target = casbin_channel_subscription ,
8887 args = (
8988 self .child_conn ,
@@ -109,9 +108,12 @@ def start(
109108 self ,
110109 timeout = 20 , # seconds
111110 ):
112- if not self .subscription_proces .is_alive ():
111+ if self .subscription_process is None :
112+ self ._create_subscription_process (start_listening = False )
113+
114+ if not self .subscription_process .is_alive ():
113115 # Start listening to messages
114- self .subscription_proces .start ()
116+ self .subscription_process .start ()
115117 # And wait for the Process to be ready to listen for updates
116118 # from PostgreSQL
117119 timeout_time = time () + timeout
@@ -124,6 +126,9 @@ def start(
124126 raise PostgresqlWatcherChannelSubscriptionTimeoutError (timeout )
125127 sleep (1 / 1000 ) # wait for 1 ms
126128
129+ def stop (self ):
130+ self ._cleanup_connections_and_processes ()
131+
127132 def _cleanup_connections_and_processes (self ) -> None :
128133 # Clean up potentially existing Connections and Processes
129134 if self .parent_conn is not None :
@@ -132,8 +137,9 @@ def _cleanup_connections_and_processes(self) -> None:
132137 if self .child_conn is not None :
133138 self .child_conn .close ()
134139 self .child_conn = None
135- if self .subscription_process is not None :
140+ if self .subscription_process is not None and self . subscription_process . pid is not None :
136141 self .subscription_process .terminate ()
142+ self .subscription_process .join ()
137143 self .subscription_process = None
138144
139145 def set_update_callback (self , update_handler : Optional [Callable [[None ], None ]]):
0 commit comments