title: how to launch a command window from Python
body: <p>I'd like to use Python 2.6 on Windows to launch several <strong>separate</strong> command windows, each running their own Python script.  The purpose is: these are clients, and I'm trying to load up the server with requests from multiple quasi-independent clients.</p>
  
  <p>I don't need to communicate with the client during or after the run, but I do need to send each a different commmandline arg, and I'd like each client's output to scroll in its own "console".</p>
  
  <p>From the DOS command line, the "start" command does what I'd like.  I can either:</p>
  
  <pre><code>start perf_test.py 2
  </code></pre>
  
  <p>or</p>
  
  <pre><code>start cmd /c perf_test.py 3
  </code></pre>
  
  <p>or</p>
  
  <pre><code>start cmd /c python perf_test.py 4
  </code></pre>
  
  <p>(These will work for you if you have your "file associations" setup correctly for *.py files.  There are other threads on that, if you need help.  Or, use full paths to the python exe and/or your script.)</p>
  
  <p>My challenge is: How do I get the same effect from Python?</p>
  
  <p>Using <code>subprocess</code> library, I've tried variations like this:</p>
  
  <pre><code>from subprocess import *
  p = Popen(["perf_test.py", "4"], shell=True, stdin=PIPE)
  </code></pre>
  
  <p>But even with <code>shell=True</code>, the output is commingled in the window I'm already running in.  Adding <code>stdout=PIPE</code> stops that, but then I have to read <code>p.stdout</code> or use <code>p.communicate()</code>.  Adding "cmd" to the Popen gets approximately the same:</p>
  
  <pre><code>p = Popen(["cmd", "/c", "perf_test.py", "4"], shell=True, stdin=PIPE)
  </code></pre>
  
  <p>None of the above achieve the effect I'm looking for, which is: "<strong>pop open a new, distinct window for this script, and watch its output scroll by in its own console</strong>" (because I really want to run N of these clients in parallel).</p>
  
  <p>One other thing I turned to <em>almost</em> works, too.</p>
  
  <pre><code>import os
  os.startfile("perf_test.py")
  </code></pre>
  
  <p>This returns immediately, and an actual dosbox pops up. Yay! Success! That is, until I try to add an argument.  This fails:</p>
  
  <pre><code>os.startfile("perf_test.py 5")
  </code></pre>
  
  <p>with error "The system cannot find the file specified"... because it is adding "[SPACE]5" to the filename.  (The purpose of the argument is that each "perf_test" needs to have an assigned ID, so that they hit the server as different instances.)</p>
  
  <p>Other approaches I've considered, and really don't like for various reasons:</p>
  
  <ul>
  <li>Run each "perf_test" in its own thread.  (But I really want to see the output each in its own console.)</li>
  <li>Make my own pseudo-consoles with Tk. (Figure I'll just hit different threading problems there.)</li>
  <li>Dynamically write a .BAT file on the fly with the lines "start perf_test.py 1", "start perf_test.py 2", etc., then launch that .BAT file with <code>Popen</code> or <code>startfile</code>.</li>
  </ul>
  
  <p>I expect the last will work... and I guess is my last resort, if I can't get a Python script to do it directly.</p>
  
  <p>Thanks for any input / insights!</p>