I am now working on Python, writing a code that need to call WINDOWS cmd.exe
with subprocess.
I would like to run thousands of processes continuously but only 6 or less processes running at the same time.
Article Control the number of subprocesses using to call external commands in python provides solutions and it seemed work fine on calling programs like Remote Desktop or something, a new program appears after one closed. But I got problem when I apply these codes on calling cmd.exe
. Codes are below:
import subprocess
from multiprocessing.pool import ThreadPool as Pool
def worker(cmd):
p = subprocess.Popen(cmd, );
p.wait()
commands = ['C:\Windows\System32\mstsc.exe',
'C:\Windows\System32\mstsc.exe',
'C:\Windows\System32\mstsc.exe',
'C:\Windows\System32\mstsc.exe',
'C:\Windows\System32\mstsc.exe',]
pool = Pool( processes = 2 );
results =[pool.apply_async(worker, [cmd]) for cmd in commands];
ans = [res.get() for res in results];
This works fine. A new window shows when one another closed.
This code works fine.
But if the processes were cmd.exe
import subprocess
from multiprocessing.pool import ThreadPool as Pool
def worker(cmd):
p = subprocess.Popen(cmd, );
p.wait()
commands = ['cmd.exe /c start "Test1" /d E:\pyworkspace\FAST\ FAST_RV_W64.exe 334.in',
'cmd.exe /c start "Test2" /d E:\pyworkspace\FAST\ FAST_RV_W64.exe 893.in',
'cmd.exe /c start "Test3" /d E:\pyworkspace\FAST\ FAST_RV_W64.exe 9527.in',
'cmd.exe /c start "Test4" /d E:\pyworkspace\FAST\ FAST_RV_W64.exe 114514.in',
'cmd.exe /c start "Test5" /d E:\pyworkspace\FAST\ FAST_RV_W64.exe 1919810.in']
pool = Pool( processes = 2 );
results =[pool.apply_async(worker, [cmd]) for cmd in commands];
ans = [res.get() for res in results];
*FAST_RV_64.exe is one of my tool program and *.in is setting file.
With calling cmd.exe
, all cmd windows jumped out at the same time and the limitation didn't work.
import subprocess
from multiprocessing.pool import ThreadPool as Pool
def worker(cmd):
p = subprocess.Popen(cmd, );
p.wait()
commands = ['cmd.exe /c start "Test1" mstsc.exe',
'cmd.exe /c start "Test2" mspaint.exe',
'cmd.exe /c start "Test3" SnippingTool.exe',
'cmd.exe /c start "Test4" mstsc.exe',
'cmd.exe /c start "Test5" mspaint.exe']
pool = Pool( processes = 2 );
results =[pool.apply_async(worker, [cmd]) for cmd in commands];
ans = [res.get() for res in results];
If I called program through cmd.exe
, it jumps out together, too.
I also tried other method in article above, but no one works.
I wonder why this happened and how can I fix my code.