【python】进程的查看和结束

4 篇文章 0 订阅

包括进程的打印和删除。

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








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学渣渣渣渣渣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值