Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4116844
  • 博文数量: 447
  • 博客积分: 1241
  • 博客等级: 中尉
  • 技术积分: 5786
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-27 06:48
个人简介

读好书,交益友

文章分类

全部博文(447)

文章存档

2023年(6)

2022年(29)

2021年(49)

2020年(16)

2019年(15)

2018年(23)

2017年(67)

2016年(42)

2015年(51)

2014年(57)

2013年(52)

2012年(35)

2011年(5)

分类: Python/Ruby

2016-08-24 20:00:37

最近使用python多线程程序,发现会耗尽所有的cpu,有必要让python程序运行在一个cpu上.,但是发现threading.get_ident() 返回一个极大的整数,根本和线程号对不上.
只是使用先获取系统调用号

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <sys/syscall.h>


  3. int main(void)
  4. {
  5.     printf("%d\n", SYS_gettid);
  6.     return 0;
  7. }


得到 186

点击(此处)折叠或打开

  1. #!/usr/bin/env python

  2. import threading
  3. import os
  4. import ctypes
  5. def cycle_burner():
  6.     SYS_gettid = 186
  7.     libc = ctypes.cdll.LoadLibrary('libc.so.6')
  8.     tid = libc.syscall(SYS_gettid)
  9.     print tid
  10.     while True:
  11.             meh = 84908230489 % 323422

  12. for i in range(3):
  13.      thread = threading.Thread(target=cycle_burner)
  14.      print "Starting a thread"
  15.      thread.start()



运行一切ok
阅读(19616) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~