Hi,

Starting the database through zdrun is quite slow, as it always ends up
sleeping for at least 1 second:
 - zdctl spawns zdrun
 - zdctl immediately tries to connect to the unix socket 
 - unix socket is not ready or non-existent
 - zdctl sleeps for 1 second
 - zdctl retries the connection

The socket is usually ready pretty quickly (especially on hot caches),
so sleeping for 1 second is quite a lot.

The attached patch speeds up startup by making zdctl launch zdrun by
fork(), and then sharing a pipe so that zdrun can tell zdctl when the
socket is ready.

It's not that pretty but it does speed up startup quite significantly. 
Would you consider this approach? If so I will update it for the latest
zdaemon (it's generated against zodb-3.3, sorry..) and clean up the
whitespace etc.

Are there any released versions of zdaemon (I note it's no longer
included in the zodb tarballs) or should I get it from svn?

Thanks.
-- 
Daniel Drake
Brontes Technologies, A 3M Company
--- zdctl.py.orig	2007-06-08 17:00:15.000000000 -0400
+++ zdctl.py	2007-06-08 17:03:03.000000000 -0400
@@ -219,10 +219,20 @@ class ZDCmd(cmd.Cmd):
             args += self._get_override("-z", "directory")
             args.extend(self.options.program)
             if self.options.daemon:
-                flag = os.P_NOWAIT
+                rpipe, wpipe = os.pipe()
+                pid = os.fork()
+                if pid == 0:
+                    os.close(rpipe)
+                    import zdrun
+                    zdrun.main(args[2:], os.fdopen(wpipe, "w"))
+                    # should never reach here
+                    sys.exit(255)
+                os.close(wpipe)
+                fd = os.fdopen(rpipe)
+                fd.read()
+                fd.close()
             else:
-                flag = os.P_WAIT
-            os.spawnvp(flag, args[0], args)
+                os.spawnvp(os.P_WAIT, args[0], args)
         elif not self.zd_pid:
             self.send_action("start")
         else:
--- zdrun.py.orig	2007-06-08 17:00:19.000000000 -0400
+++ zdrun.py	2007-06-08 17:02:27.000000000 -0400
@@ -235,10 +235,11 @@ class Subprocess:
 
 class Daemonizer:
 
-    def main(self, args=None):
+    def main(self, args=None, close_fd=None):
         self.options = ZDRunOptions()
         self.options.realize(args)
         self.logger = self.options.logger
+        self.close_fd = close_fd
         self.set_uid()
         self.run()
 
@@ -296,6 +297,9 @@ class Daemonizer:
         sock.listen(1)
         sock.setblocking(0)
         self.mastersocket = sock
+        if self.close_fd is not None:
+        	self.close_fd.close()
+        	self.close_fd = None
 
     def unlink_quietly(self, filename):
         try:
@@ -709,11 +713,11 @@ def get_path():
     return path
 
 # Main program
-def main(args=None):
+def main(args=None, close_fd=None):
     assert os.name == "posix", "This code makes many Unix-specific assumptions"
 
     d = Daemonizer()
-    d.main(args)
+    d.main(args, close_fd)
 
 if __name__ == "__main__":
     main()
_______________________________________________
For more information about ZODB, see the ZODB Wiki:
http://www.zope.org/Wikis/ZODB/

ZODB-Dev mailing list  -  ZODB-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zodb-dev

Reply via email to