查看python 子进程的信息:
import multiprocessing
import psutil
import time
import os
def worker(file_info, interval):
file_info_cp = file_info
this_thread = multiprocessing.current_process()
print('worker %s: PID=%s' % (this_thread.name, this_thread.pid))
tp = psutil.Process(this_thread.pid)
print('worker %s: memory_info=%s' % (this_thread.name, tp.memory_info().rss/1024/1024))
n = 1000000000
while n > 0:
print("%s The time is {0}".format(time.ctime()) % this_thread.name)
time.sleep(interval)
n -= 1
def make_multi_process():
with open('test.txt', mode='r', encoding='utf8') as f:
file_info = f.read()
print('sss b')
worker_num = 2
interval = 3
this_thread = multiprocessing.current_process()
tp = psutil.Process(this_thread.pid)
print('main worker %s: PID=%s' % (this_thread.name, this_thread.pid))
print('main worker %s: memory_info=%s' % (this_thread.name, tp.memory_info().rss / 1024 / 1024))
for i in range(worker_num):
entity_profiler_proc = multiprocessing.Process(target=worker, args=(file_info, interval), name='M_thread_%d' % i)
#entity_profiler_proc.daemon = True
entity_profiler_proc.start()
print('sss e')
if __name__ == "__main__":
make_multi_process()