包括进程的打印和删除。
import psutil
import os
import signal
print("----------------------------- show all processes info --------------------------------")
# show processes info
pids = psutil.pids()
for pid in pids:
p = psutil.Process(pid)
# get process name according to pid
process_name = p.name()
print("Process name is: %s, pid is: %s" %(process_name, pid))
print("----------------------------- kill specific process --------------------------------")
pids = psutil.pids()
for pid in pids:
p = psutil.Process(pid)
# get process name according to pid
process_name = p.name()
# kill process "sleep_test1"
if 'sleep_test1' == process_name: # 杀死叫做 sleep_test1 的进程
print("kill specific process: name(%s)-pid(%s)" %(process_name, pid))
os.kill(pid, signal.SIGKILL)
多线程实战
有时候想要同时运行多个 exe 文件,代码如下:
import psutil
import os
import signal
import time
import argparse
parser = argparse.ArgumentParser()
# ========================= federated learning parameters ========================
parser.add_argument('--max_num', type=int, default=3,
help="number of processes: K")
args = parser.parse_args()
import multiprocessing
import os
import threading
def task_one(exe_name):
main = os.path.join(os.path.dirname(__file__), exe_name)
try:
f = os.popen(main)
print(f.read())
except Exception as e:
print(e)
class MyThread(threading.Thread):
def run(self):
os.system("shualiuliang.exe")
pass
args.max_num = args.max_num * 2
while True:
print("----检测进程(20S执行一次捏)----")
# show processes info
pid_num = 0
pids = psutil.pids()
for pid in pids:
p = psutil.Process(pid)
# get process name according to pid
if p.name() == "shualiuliang.exe":
pid_num += 1
print(f"现在有{pid_num/2}个进程捏")
if pid_num < args.max_num:
num_run = args.max_num - pid_num
print("进程少了,现在加载")
for i in range(num_run):
thread = MyThread()
thread.daemon = True
thread.start()
time.sleep(1)
print("重启了一个捏")
time.sleep(20)
自动kill进程的爬虫代码
为了防止进程卡住,自动在运行后的几秒内kill所有进程并重新运行进程:
import psutil
import os
import signal
import time
import argparse
parser = argparse.ArgumentParser()
# ========================= federated learning parameters ========================
parser.add_argument('--max_num', type=int, default=5,
help="number of processes: K")
args = parser.parse_args()
import multiprocessing
import os
import threading
def task_one(exe_name):
main = os.path.join(os.path.dirname(__file__), exe_name)
try:
f = os.popen(main)
print(f.read())
except Exception as e:
print(e)
class MyThread(threading.Thread):
def run(self):
os.system("shualiuliang.exe")
pass
args.max_num = args.max_num
#while True:
# try:
check_num = 0
while True:
check_num += 1
while True:
try:
print("----检测进程(20S执行一次捏)----")
# show processes info
pid_num = 0
pids = psutil.pids()
for pid in pids:
p = psutil.Process(pid)
# get process name according to pid
if p.name() == "shualiuliang.exe":
pid_num += 1
pid_num = int(pid_num/2)
print(f"现在有{pid_num}个进程捏")
if pid_num < args.max_num:
num_run = args.max_num - pid_num
print("进程少了,现在加载")
for i in range(num_run):
thread = MyThread()
thread.daemon = True
thread.start()
time.sleep(1)
print("重启了一个捏")
except:
pass
else:
break
time.sleep(20)
if check_num % 10 == 0:
while True:
try:
pids = psutil.pids()
for pid in pids:
p = psutil.Process(pid)
p_name = p.name()
if 'shualiuliang.exe' == p_name: # 杀死叫做 sleep_test1 的进程
print("Kill specific process: name(%s)-pid(%s)" %(p_name, pid))
os.kill(pid, signal.SIGINT)
except:
pass
else:
break
# except:
# pass