栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python程序使用psutil采集设备信息,通过共享内存发送至C++程序

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python程序使用psutil采集设备信息,通过共享内存发送至C++程序

Python程序使用psutil采集设备信息,通过共享内存发送至C++程序

##仓库路径: https://gitee.com/liudegui/py_cpp_psutil

说明

Python程序使用psutil采集设备信息,通过共享内存发送至C++程序;
需基于python3.8+版本;
使用到了boost库的共享内存库。

C++端
#include 
#include 
#include "shm_buf.h"
#include 

void producer()
{
    const char *shm_name = "edge_client_cpp_py";
    SharedMemoryBuffer shmbuf(shm_name);

    while (true)
    {
        if (!shmbuf.readable())
            sleep(1);
        std::string read_data;
        shmbuf.read_shm(read_data);
        std::cout << "read_data: " << read_data << std::endl;
    }
}

int main(int argc, char *argv[])
{
    producer();
    return 0;
}
Python端
# -*- coding: utf-8 -*-

import time
import json
from shm_buf import SharedMemoryBuffer
import psutil
import datetime

def brief_info():
    cpu_percent = psutil.cpu_percent()
    memory_percent = psutil.virtual_memory()[2]
    disk_info = ""
    for part in psutil.disk_partitions(all=False):
        usage = psutil.disk_usage(part.mountpoint)
        disk_info += str("  %s used %s percent. n" % (part.device, usage.percent))
    boot_time = datetime.datetime.fromtimestamp(psutil.boot_time())
    running_since = boot_time.strftime("%A %d. %B %Y")
    
    t = {}
    t["cpu_percent"] = cpu_percent
    t["memory_percent"] = memory_percent
    t["disk_partitions"] = psutil.disk_partitions(all=False)
    t["boot_time"] = str(boot_time)
    return t


def timing_send_sysinfo():
    shm_name = "edge_client_cpp_py"
    shm_buf = SharedMemoryBuffer(shm_name = shm_name, create_shm = True)
    
    while True:
        info = brief_info()
        shm_buf.write_shm(json.dumps(info, ensure_ascii=False).encode('utf-8'))
        time.sleep(3)


if __name__ == "__main__":
    timing_send_sysinfo()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/998359.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号