Index: tracdarcs/command.py
===================================================================
--- tracdarcs/command.py	(revision 168)
+++ tracdarcs/command.py	(revision 173)
@@ -44,4 +44,8 @@
     A simple wrapper around the 'darcs' command.
     '''
+
+    RUNNING_DARCSES = None
+    darcs_version = None
+
     def __init__(self, darcs_bin, repo_dir, log, possible_encodings):
         self.darcs_bin = darcs_bin
@@ -49,5 +53,9 @@
         self.log = log
         self.possible_encodings = possible_encodings
-        self.darcs_version = self._run('--version').strip()
+
+        # Determine the darcs version just once
+        if DarcsCommand.darcs_version is None:
+            DarcsCommand.darcs_version = self._run('--version').strip()
+
         if self.darcs_version >= '2.4':
             self.issue183 = False
@@ -71,7 +79,22 @@
         else:
             command = 'TZ=UTC ' + command
-        if self.log:
-            self.log.debug(command)
-        np = NaivePopen(command, input=input, capturestderr=True)
+
+        # Respect the maximum number of running darcs limit
+        if DarcsCommand.RUNNING_DARCSES is not None:
+            if self.log:
+                self.log.debug('Asking permission to run %s ...', command)
+            DarcsCommand.RUNNING_DARCSES.acquire()
+            if self.log:
+                self.log.debug('... got permission to run %s', command)
+        else:
+            if self.log:
+                self.log.debug(command)
+
+        try:
+            np = NaivePopen(command, input=input, capturestderr=True)
+        finally:
+            if DarcsCommand.RUNNING_DARCSES is not None:
+                DarcsCommand.RUNNING_DARCSES.release()
+
         if np.errorlevel:
             err = 'Running (%s) failed: %s, %s: %s' % (command,
Index: tracdarcs/components.py
===================================================================
--- tracdarcs/components.py	(revision 170)
+++ tracdarcs/components.py	(revision 173)
@@ -37,4 +37,7 @@
     darcs_command = Option('darcs', 'command', 'darcs',
                            "Name of the external darcs binary.")
+
+    max_concurrent_darcses = Option('darcs', 'max_concurrent_darcses', 'false',
+                                    "Max number of concurrent darcses running per repository.")
 
     possible_encodings = Option('darcs', 'possible_encodings', 'utf-8,iso8859-1',
@@ -56,4 +59,14 @@
             possible_encodings = [e.strip()
                                   for e in self.possible_encodings.split(',')]
+
+        # Setup the semaphore used to limit the number of concurrent running
+        # darcs within a single repository.
+
+        if self.max_concurrent_darcses and int(self.max_concurrent_darcses)>0:
+            from command import DarcsCommand
+            if DarcsCommand.RUNNING_DARCSES is None:
+                from threading import BoundedSemaphore
+                DarcsCommand.RUNNING_DARCSES = BoundedSemaphore(value=int(self.max_concurrent_darcses))
+
         return DarcsRepository(db, dir, self.env.log, darcs, possible_encodings, params)
 
