how to assign pid to process code example
Example: can you assign a pid to a process via python
def main():
proc_info_old = get_proc_info()
pids_old = get_all(proc_info_old, "ProcessId")
os.system('start notepad.exe')
proc_info_new = get_proc_info()
pids_new = get_pids(proc_info_new, "ProcessId")
new_pids = list(set(pids_new) - set(pids_old))
for pid in new_pids:
cmd = get_for(proc_info_new, "ProcessId", pid, "CommandLine")
test.log("PID: %s" % pid)
test.log("CMD: %s" % cmd)
test.log("")
os.system("taskkill /f /im notepad.exe")
def get_all(proc_info, get_name):
res = []
for pi in proc_info:
res.append(pi[get_name])
return res
def get_for(proc_info, look_for_name, look_for_value, get_name):
for pi in proc_info:
if pi[look_for_name] == look_for_value:
return pi[get_name]
return None
def get_proc_info(search_expression=None):
if search_expression is None:
search_expression = ""
else:
search_expression = " WHERE " + search_expression
s = 'pushd "' + os.getcwd() + '" && wmic PROCESS ' + search_expression + ' GET * /format:csv <nul'
d = subprocess.Popen(s, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0];
d = d[3:]
fn = "temp.csv"
f = codecs.open(fn, "w", "utf8")
f.write(d)
f.close()
dataset = testData.dataset(fn)
all_proc_info = []
for row in dataset:
proc_info = {}
field_names = testData.fieldNames(row)
for n in field_names:
v = testData.field(row, n)
proc_info[n] = v
all_proc_info.append(proc_info)
os.remove(fn)
return all_proc_info