title: Share mercurial command server between multiple processes
body: <p>I'm using Mercurial to create repositories based on other repositories (i.e. merging them together). This is done by a nice little webinterface using PHP on the server.</p>
  
  <p>Unfortunately, for some tasks this is extremely slow (no wonder, PHP currently has to exec() hg about 40 times per request). So I thought about using the command server, available in Mercurial 1.9+.</p>
  
  <p>As far as the docs go, I can start a simple <code>hg serve</code> and let it work on multiple repositories (by giving each command the <code>-R</code> option). This is great. But can I also share the command server between mutliple (and maybe concurrent) requests?</p>
  
  <p>I'm doing something like this to start the server from within my PHP script:</p>
  
  <pre><code>$cmd         = 'hg serve --cmdserver pipe';
  $pipes       = null;
  $descriptors = array(
     STDIN  =&gt; array('pipe', 'r'),
     STDOUT =&gt; array('pipe', 'w')
  );
  
  $process = proc_open($cmd, $descriptors, $pipes);
  
  if (!is_resource($process)) {
      throw new Exception('Could not start command server.');
  }
  </code></pre>
  
  <p>But this makes the server only available to the current PHP process. Since I'm not familiar with using pipes and stuff, I now wonder: Is it possible to start the server once (outside PHP) and "connect" to it? Like opening a socket and reading/writing to it.</p>
  
  <p>And if it's possible: Is it wise? What happens when multiple PHP processes start using the one server's socket/pipe/whatever?</p>