python - Why are os.system and subprocess.call spawning so many processes? -
import os import subprocess import sys import re ## fname_ext=sys.argv[1] fname_ext=r"c:\mine\.cs\test.cs" exe=os.path.splitext(fname_ext)[0]+".exe" # executable fdir=os.path.split(fname_ext)[0] fcontent=open(fname_ext).read() p_using=re.compile("\s*using\s+((\w+[.]*)+)") p_namespace=re.compile("\s*namespace\s+(\w+)") usings=p_using.findall(fcontent) usings=[x[0] x in usings] references=[] in os.listdir(fdir): path=fdir+"\\"+i try: if os.path.isdir(path) or (not path.endswith('cs')):continue open(path) fp: content=fp.read() namespaces=p_namespace.findall(content) n in namespaces: if n in usings , 'system' not in n: references+=[path] except: pass command="csc /nologo "+" ".join(references)+" "+fname_ext ## command=" ".join(references) #~ --------------------------------------------------------- # build: option=1 if option==0: # using os.system print ">>",command if os.system(command)==0: os.system(exe) else: #~ using subprocess module ## print type(references) command=['csc'] ## print command,references command.extend(["/nologo","/out:"+exe]) command.extend(references) command.append(fname_ext) ## print command if subprocess.call(command,shell=true)==0: ## print "running %s"%exe subprocess.call([exe],shell=true) else: pass ## print "failed run" #~ ---------------------------------------------------------
i have code above supposed run csharp program scite
. searches
every .cs
file in directory , finds file namespace current
file has included. command run file in scite is:
command.go.*.cs=python c:\mine\.py\csc.py $(filepath)
command.go.subsystem.*.cs=0
that program logic part okay.
issue when hit f5 sample csharp code this:
using system; using system.collections; using mynamespace; class test{ public static void main(string[] args){ myobject inst=new myobject(); myobject.self_destruct(inst); } }
it runs ok. when uncomment second fname_ext
, comment first one
, run csc.py file, window opens , keeps running, printing command
(this happens
using os.system
option). when use the subprocess.call
option, same thing
happens time when shell=true
. ran 15 seconds , there 800+ cmd.exe , python.exe processes.i had wait 5 minutes after killing cmd.exe
mouse start responding , 2 minutes more desktop peek work.
when shell=false
, runs ok, same way when hit f5 key file.
what happening here?
what shell=true
doing makes behave way?
okay, i'll take stab @ this. if understand situation, script called csc.py , want call csc c# compiler. when run csc /nologo (etc...)
through cmd.exe, starts looking called 'csc' known extension. finds csc.py in current directory , since .py registered extension, that's gets executed.
the solution rename python file or call out 'csc.exe' explicitly.
Comments
Post a Comment