写了一个简单的多线程程序
(python 2.4)
(4核server)
#!/usr/bin/env python
# -*- coding:gb2312 -*-
import sys
import time
import threading
import os
def run(arg):
pid = os.getpid()
print "This thread id:%d" % pid
print "msg:",arg
ret = 0
while True:
#time.sleep(1)
ret += 1
print "Parent pid: %d" % os.getpid()
for i in range(0,4):
msg = "index %d" % i
ps = threading.Thread(target = run,args =(msg,))
ps.start()
输出:
Parent pid: 7749
This thread id:7751
msg: index 0
This thread id:7752
msg: index 1
This thread id:7753
msg: index 2
This thread id:7754
msg: index 3
pstree -p 输出:
|-sshd2(18660)-+-sshd2(31624)---bash(31626)---python(7549)---python(7550)-+-python(7551)
| | |-python(7552)
| | |-python(7553)
| | `-python(7554)
| `-sshd2(3634)---bash(3636)---pstree(7555)
top,每个线程占用30左右的cpu
问题:
1.python线程模型是哪种?
Linux 线程模型的比较:LinuxThreads 和NPTL2.为什么每个线程系统占用率不是很高(增加到100线程,每个线程占用 cpu比例为2%)
阅读(1489) | 评论(0) | 转发(0) |