经简单测试,代码如下
多线程测试文件名multh.py :
from threading import Thread import os,time def countc(): i =0 while i <100: i = i+1 time.sleep(10) print('module name:', __name__) print('parent process:', os.getppid()) print('process id:', os.getpid()) if __name__=='__main__': t1 = Thread(target=countc) t2 = Thread(target=countc) t3 = Thread(target=countc) t4 = Thread(target=countc) t5 = Thread(target=countc) t6 = Thread(target=countc) t1.start() t2.start() t3.start() t4.start() t5.start() t6.start() t1.join() t2.join() t3.join() t4.join() t5.join() t6.join()
多进程测试代码:
from multiprocessing import Process,Pool import os,time def countc(): i =0 while i <10: i = i+1 time.sleep(10) print('module name:', __name__) print('parent process:', os.getppid()) print('process id:', os.getpid()) if __name__=='__main__': p=Pool(60) for i in range(60): p.apply_async(countc,) p.close() p.join()
bdi@hebsjzx-hebei-bdi-83-81:/data/script/hbdb> ps -eF|grep -v grep|egrep "UID|multh.py"|awk '{print}'|sort -nk7 UID PID PPID C SZ RSS PSR STIME TTY TIME CMD bdi 184780 29989 0 44752 7508 4 16:41 pts/4 00:00:00 python multh.py
ps 的结果中,PSR 列是processor序号
结论:1.多线程使用一个进程id,只使用linux多个核心中的一个核心(processor)
2.多进程使用多个processor,进程id和processor号是无交叉的多对多关系,即 多个进程可使用同一个processor,所有进程对于多个processor
补充:cat /proc/cpuinfo
physical id : 0
physical id : 1
表示两个物理cpu块
core id 0-11 共12个核心
processor 0-47 共48个逻辑处理器(或者叫cpu线程,不清楚网上为什么这么称呼)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)