python3 获取 进程id 线程id

1.获取线程id

import threading
 
# 1 获取线程ID,NAME
t = threading.currentThread()
#线程ID
print('Thread id : %d' % t.ident)
#线程NAME
print('Thread name : %s' % t.getName())

输出:

Thread id : 7080
Thread name : MainThread

2.获取进程id

import psutil
import os
import datetime
 
pid = os.getpid()
p = psutil.Process(pid)
print('----------------')
#进程ID
print('Process id : %d' % pid)
#进程NAME
print('Process name : %s' % p.name())
#获取进程bin路径
print('Process bin  path : %s' % p.exe())
#获取pid对应的路径
print('Process path : %s' % p.cwd())
#进程状态
print('Process status : %s' % p.status())
#进程运行时间
print('Process creation time : %s' % datetime.datetime.fromtimestamp(p.create_time()).strftime("%Y-%m-%d %H:%M:%S"))
#CPU使用情况
print(p.cpu_times())
#内存使用情况
print('Memory usage : %s%%' % p.memory_percent())
#硬盘读取信息
print(p.io_counters())
#打开进程socket的namedutples列表
print(p.connections())
#此进程的线程数
print('Process number of threads : %s' % p.num_threads())

输出:

Process id : 3136
Process name : python.exe
Process bin  path : D:\Programs\Anaconda\python.exe
Process path : D:\1_practice\python\projects\python_accumulate\function\6_并发\线程
Process status : running
Process creation time : 2018-09-04 19:45:39
pcputimes(user=0.062400399999999995, system=0.1092007)
Memory usage : 0.0903536933504%
pio(read_count=507L, write_count=28L, read_bytes=793919L, write_bytes=399L)
[]
Process number of threads : 2

你可能感兴趣的:(python,开发语言)