speechd-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Make SSIPClient not lock program exit even if not closed, when t


From: Rui Batista
Subject: [PATCH] Make SSIPClient not lock program exit even if not closed, when terminating calling application
Date: Fri, 15 Jul 2011 15:09:39 +0100

In the case of some application not closing a SSIPClient instance
with SSIPClient.close(), the interpreter could run forever waiting for data
on the _ssip_connection auxiliary thread.
This patch closes the connection on the __del__ method, if not already closed.

Moreover the _ssip_connection auxiliary thread is set to a daemon thread
meaning it is terminated when all non-daemon threads are terminated in the 
calling application.
---
 src/api/python/speechd/client.py |   14 +++++++++++++-
 1 files changed, 13 insertions(+), 1 deletions(-)

diff --git a/src/api/python/speechd/client.py b/src/api/python/speechd/client.py
index 13a9c7e..597d432 100644
--- a/src/api/python/speechd/client.py
+++ b/src/api/python/speechd/client.py
@@ -199,6 +199,8 @@ class _SSIP_Connection(object):
         self._communication_thread = \
                 threading.Thread(target=self._communication, kwargs={},
                                  name="SSIP client communication thread")
+        # Make the thread exit if all main threads terminate
+        self._communication_thread.setDaemon(True)
         self._communication_thread.start()
     
     def close(self):
@@ -212,7 +214,8 @@ class _SSIP_Connection(object):
         self._socket.close()
         # Wait for the other thread to terminate
         self._communication_thread.join()
-        
+        self._communication_thread = None
+
     def _communication(self):
         """Handle incomming socket communication.
 
@@ -383,6 +386,15 @@ class _SSIP_Connection(object):
         """
         self._callback = callback
 
+    def __del__(self):
+        # If client is not already closed close before deleting.
+        if self._communication_thread:
+            try:
+                self.close()
+            except:
+                pass
+
+
 class _CallbackHandler(object):
     """Internal object which handles callbacks."""
 
-- 
1.7.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]