[Checkins] SVN: zc.ngi/branches/jim-dev/src/zc/ngi/ Don't start async thread on import. Start it when needed or via an

Jim Fulton jim at zope.com
Thu Oct 1 07:07:42 EDT 2009


Log message for revision 104671:
  Don't start async thread on import.  Start it when needed or via an
  api. The api allows you to run the thread in non-daemon mode.
  

Changed:
  U   zc.ngi/branches/jim-dev/src/zc/ngi/async.py
  U   zc.ngi/branches/jim-dev/src/zc/ngi/tests.py

-=-
Modified: zc.ngi/branches/jim-dev/src/zc/ngi/async.py
===================================================================
--- zc.ngi/branches/jim-dev/src/zc/ngi/async.py	2009-10-01 11:07:39 UTC (rev 104670)
+++ zc.ngi/branches/jim-dev/src/zc/ngi/async.py	2009-10-01 11:07:41 UTC (rev 104671)
@@ -256,6 +256,8 @@
         _CONNECT_OK          = (0, errno.EISCONN)
 
     def __init__(self, addr, handler):
+        if not _thread:
+            start_thread()
         self.__handler = handler
         if isinstance(addr, str):
             sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
@@ -351,6 +353,8 @@
     logger = logging.getLogger('zc.ngi.async.server')
 
     def __init__(self, addr, handler):
+        if not _thread:
+            start_thread()
         self.__handler = handler
         self.__close_handler = None
         self.__connections = {}
@@ -447,6 +451,8 @@
     connected = True
 
     def __init__(self, addr, handler, buffer_size=4096):
+        if not _thread:
+            start_thread()
         self.__handler = handler
         self.__buffer_size = buffer_size
         asyncore.dispatcher.__init__(self)
@@ -640,6 +646,16 @@
             logger.exception('loop error')
             raise
 
-_thread = threading.Thread(target=loop, name=__name__)
-_thread.setDaemon(True)
-_thread.start()
+_thread = None
+_start_lock = threading.Lock()
+def start_thread(daemon=True):
+    global _thread
+    _start_lock.acquire()
+    try:
+        if _thread is not None:
+            return
+        _thread = threading.Thread(target=loop, name=__name__)
+        _thread.setDaemon(daemon)
+        _thread.start()
+    finally:
+        _start_lock.release()

Modified: zc.ngi/branches/jim-dev/src/zc/ngi/tests.py
===================================================================
--- zc.ngi/branches/jim-dev/src/zc/ngi/tests.py	2009-10-01 11:07:39 UTC (rev 104670)
+++ zc.ngi/branches/jim-dev/src/zc/ngi/tests.py	2009-10-01 11:07:41 UTC (rev 104671)
@@ -24,6 +24,8 @@
 import zc.ngi.testing
 import zc.ngi.wordcount
 
+zc.ngi.async.start_thread() # Make sure the thread is already running
+
 def test_async_cannot_connect():
     """Let's make sure that the connector handles connection failures correctly
 



More information about the checkins mailing list