Chinaunix首页 | 论坛 | 博客
  • 博客访问: 113571
  • 博文数量: 15
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 425
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-01 14:51
文章分类

全部博文(15)

文章存档

2013年(4)

2011年(1)

2010年(2)

2009年(2)

2008年(6)

我的朋友

分类: Python/Ruby

2008-03-14 10:05:26

写了一个简单的多线程程序
(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 和NPTL
2.为什么每个线程系统占用率不是很高(增加到100线程,每个线程占用 cpu比例为2%)

阅读(1425) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~