1. 程式人生 > 其它 >python C語言擴充套件之簡單擴充套件-使用ctypes訪問C程式碼

python C語言擴充套件之簡單擴充套件-使用ctypes訪問C程式碼

對於需要呼叫C程式碼的一些小的問題,通常使用Python標準庫中的 ctypes 模組就足夠了。 要使用 ctypes ,你首先要確保你要訪問的C程式碼已經被編譯到和Python直譯器相容 (同樣的架構、字大小、編譯器等)的某個共享庫中。
這裡有個c語言讀取linux下cpu很memory相關的c程式碼 ,目的是想要在Python中訪問


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <unistd.h>


typedef struct MEMPACKED         //定義一個mem occupy的結構體
{
    char name1[20];      //定義一個char型別的陣列名name有20個元素
    unsigned long MemTotal;
    char name2[20];
    unsigned long MemFree;
    char name3[20];
    unsigned long Buffers;
    char name4[20];
    unsigned long Cached;
    char name5[20];
    unsigned long SwapCached;
}MEM_OCCUPY;

//proc/stat檔案結構
//cpu  633666 46912 249878 176813696 782884 2859 19625 0
//cpu0 633666 46912 249878 176813696 782884 2859 19625 0
//intr 5812844
//ctxt 265816063
//btime 1455203832
//processes 596625
//procs_running 1
//procs_blocked 0

typedef struct CPUPACKED         //定義一個cpu occupy的結構體
{
    char name[20];      //定義一個char型別的陣列名name有20個元素
    unsigned int user; //定義一個無符號的int型別的user
    unsigned int nice; //定義一個無符號的int型別的nice
    unsigned int system;//定義一個無符號的int型別的system
    unsigned int idle; //定義一個無符號的int型別的idle
    unsigned int lowait;
    unsigned int irq;
    unsigned int softirq;
}CPU_OCCUPY;


void get_memoccupy(MEM_OCCUPY *mem) //對無型別get函式含有一個形參結構體類弄的指標O
{
    FILE *fd;
    char buff[256];
    MEM_OCCUPY *m;
    m = mem;

    fd = fopen("/proc/meminfo", "r");
    //MemTotal: 515164 kB
    //MemFree: 7348 kB
    //Buffers: 7892 kB
    //Cached: 241852  kB
    //SwapCached: 0 kB
    //從fd檔案中讀取長度為buff的字串再存到起始地址為buff這個空間裡
    fgets(buff, sizeof(buff), fd);
    sscanf(buff, "%s %lu ", m->name1, &m->MemTotal);
    fgets(buff, sizeof(buff), fd);
    sscanf(buff, "%s %lu ", m->name2, &m->MemFree);
    fgets(buff, sizeof(buff), fd);
    sscanf(buff, "%s %lu ", m->name3, &m->Buffers);
    fgets(buff, sizeof(buff), fd);
    sscanf(buff, "%s %lu ", m->name4, &m->Cached);
    fgets(buff, sizeof(buff), fd);
    sscanf(buff, "%s %lu", m->name5, &m->SwapCached);

    fclose(fd);     //關閉檔案fd
}


int get_cpuoccupy(CPU_OCCUPY *cpust) //對無型別get函式含有一個形參結構體類弄的指標O
{
    FILE *fd;
    char buff[256];
    CPU_OCCUPY *cpu_occupy;
    cpu_occupy = cpust;

    fd = fopen("/proc/stat", "r");
    fgets(buff, sizeof(buff), fd);

    sscanf(buff, "%s %u %u %u %u %u %u %u", cpu_occupy->name, &cpu_occupy->user, &cpu_occupy->nice, &cpu_occupy->system, &cpu_occupy->idle, &cpu_occupy->lowait, &cpu_occupy->irq, &cpu_occupy->softirq);


    fclose(fd);

    return 0;
}


void cal_cpuoccupy(CPU_OCCUPY *o, CPU_OCCUPY *n,double *result)
{
    unsigned long od, nd;
    double cpu_use = 0;

    od = (unsigned long)(o->user + o->nice + o->system + o->idle + o->lowait + o->irq + o->softirq);//第一次(使用者+優先順序+系統+空閒)的時間再賦給od
    nd = (unsigned long)(n->user + n->nice + n->system + n->idle + n->lowait + n->irq + n->softirq);//第二次(使用者+優先順序+系統+空閒)的時間再賦給od
    double sum = nd - od;
    double idle = n->idle - o->idle;
    cpu_use = idle / sum;
    idle = n->user + n->system + n->nice - o->user - o->system - o->nice;
    cpu_use = idle / sum;
    *result = cpu_use;
//    printf("%.3f\n",cpu_use);
}
void cpu_use_percent(double*cpu_use)
{
    CPU_OCCUPY cpu_stat1;
    CPU_OCCUPY cpu_stat2;

    get_cpuoccupy((CPU_OCCUPY *)&cpu_stat1);
    usleep(100000);
    get_cpuoccupy((CPU_OCCUPY *)&cpu_stat2);
    cal_cpuoccupy((CPU_OCCUPY *)&cpu_stat1, (CPU_OCCUPY *)&cpu_stat2,cpu_use);

}
void mem_use_percent(double *use)
{
   MEM_OCCUPY mem_stat;
   get_memoccupy((MEM_OCCUPY *)&mem_stat);
   *use =  mem_stat.MemFree * 1.0 / (mem_stat.MemTotal * 1.0);
}

int main1( int argc, char **argv )
{
    MEM_OCCUPY mem_stat;
    CPU_OCCUPY cpu_stat1;
    CPU_OCCUPY cpu_stat2;
    double result;
    //獲取記憶體
    //(MemTotal - MemFree)/ MemTotal
    get_memoccupy((MEM_OCCUPY *)&mem_stat);
    //printf(" [MemTotal] = %lu \n [MemFree] = %lu \n [Buffers] = %lu \n [Cached] = %lu \n [SwapCached] = %lu \n", mem_stat.MemTotal, mem_stat.MemFree, mem_stat.Buffers, mem_stat.Cached, mem_stat.SwapCached);
    printf("%.3f\n", mem_stat.MemFree * 1.0 / ( mem_stat.MemTotal * 1.0  ) );
    //第一次獲取cpu使用情況
    get_cpuoccupy((CPU_OCCUPY *)&cpu_stat1);

    usleep(100000);

    //第二次獲取cpu使用情況
    get_cpuoccupy((CPU_OCCUPY *)&cpu_stat2);
    //計算cpu使用率
    cal_cpuoccupy((CPU_OCCUPY *)&cpu_stat1, (CPU_OCCUPY *)&cpu_stat2,&result);
  printf("cpu_use:%.3f ",result);
    return 0;
}
int main2()
{
    double cpu_use;
    double mem_use;
    mem_use_percent(&mem_use);
    printf("mem_use:%.3f\n",mem_use);
    cpu_use_percent(&cpu_use);
    printf("cpu_use:%.3f \n",cpu_use);

}

使用GCC編譯成libinfo.so

gcc -fPIC -shared test_cpu_info.c -o libinfo.so

然後就是在Python中載入

import ctypes
import os

# Try to locate the .so file in the same directory as this file
_file = 'libinfo.so'
base_dir = os.getcwd()
_path =os.path.join(base_dir,_file)#這個路徑是so檔案的絕對路徑
_mod = ctypes.cdll.LoadLibrary(_path)
_mem_use_percent = _mod.mem_use_percent
_mem_use_percent.argtypes= (ctypes.POINTER(ctypes.c_double),)#這裡必須是一個元組序列 否則會報錯

def mem_use_percent():
    mem_p = ctypes.c_double()
    _mem_use_percent(mem_p)
    print(mem_p)

下面是執行這個函式的程式碼