Dla Windows
sposób, aby uzyskać wszystkie PID programów na komputerze bez pobierania żadnych modułów:
import os
pids = []
a = os.popen("tasklist").readlines()
for x in a:
try:
pids.append(int(x[29:34]))
except:
pass
for each in pids:
print(each)
Jeśli tylko chciał jeden program lub wszystkie programy o tej samej nazwie i chciałeś zabić proces lub coś:
import os, sys, win32api
tasklistrl = os.popen("tasklist").readlines()
tasklistr = os.popen("tasklist").read()
print(tasklistr)
def kill(process):
process_exists_forsure = False
gotpid = False
for examine in tasklistrl:
if process == examine[0:len(process)]:
process_exists_forsure = True
if process_exists_forsure:
print("That process exists.")
else:
print("That process does not exist.")
raw_input()
sys.exit()
for getpid in tasklistrl:
if process == getpid[0:len(process)]:
pid = int(getpid[29:34])
gotpid = True
try:
handle = win32api.OpenProcess(1, False, pid)
win32api.TerminateProcess(handle, 0)
win32api.CloseHandle(handle)
print("Successfully killed process %s on pid %d." % (getpid[0:len(prompt)], pid))
except win32api.error as err:
print(err)
raw_input()
sys.exit()
if not gotpid:
print("Could not get process pid.")
raw_input()
sys.exit()
raw_input()
sys.exit()
prompt = raw_input("Which process would you like to kill? ")
kill(prompt)
to była tylko pastę mojego programu kill proces mogłem zrobić to o wiele lepiej, ale to jest w porządku.
Jeśli chcesz pracować krzyż -platform (np. równie dobrze w systemach Linux, Mac, Solaris, ...) nie ma lepszego sposobu niż parsowanie wyjścia 'pf'. Jeśli jest to przeznaczone dla pojedynczej, bardzo specyficznej platformy, edytuj swoje Q, aby dodać tę oczywiście kluczową informację (dokładne wersje systemu operacyjnego, na które musisz trafić) * i * także tag! –
Możesz odczytywać dane wyjściowe pliku ps bezpośrednio w pythonie – Mark