python编程测试电脑开启最大线程数实例代码

yipeiwu_com6年前Python基础

本文实例代码主要实现python编程测试电脑开启最大线程数,具体实现代码如下。

#!/usr/bin/env python  
#coding=gbk  
  
import threading  
import time, random, sys  
  
class Counter:  
  def __init__(self):  
    self.lock = threading.Lock()  
    self.value = 0  
  
  def increment(self):  
    self.lock.acquire()   
    self.value = value = self.value + 1  
    self.lock.release()  
    return value  
  
counter = Counter()  
cond = threading.Condition()  
  
class Worker(threading.Thread):  
  
  def run(self):  
    print self.getName(), "-- created."  
    cond.acquire()  
    #for i in range(10):  
      # pretend we're doing something that takes 10?00 ms  
      #value = counter.increment()   
      # time.sleep(random.randint(10, 100) / 1000.0)  
    cond.wait()  
    #print self.getName(), "-- task", "finished"   
    cond.release()  
      
  
  
if __name__ == '__main__':  
    
    try:  
      for i in range(3500):  
        Worker().start() # start a worker  
    except BaseException, e:  
      print "异常: ", type(e), e  
      time.sleep(5)  
      print "maxium i=", i  
    finally:  
      cond.acquire()  
      cond.notifyAll()  
      cond.release()  
      time.sleep(3)  
      print threading.currentThread().getName(), " quit" 

运行结果部分截图:

总结

以上就是本文关于python编程测试电脑开启最大线程数实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

基于Python的微信机器人开发 微信登录和获取好友列表实现解析

基于Python的微信机器人开发 微信登录和获取好友列表实现解析

首先需要安装itchat库,可以pip install itchat安装,也可以在pycharm里安装 # -*- coding:utf-8 -*- __author__ = "Mu...

对python中的xlsxwriter库简单分析

一、xlsxwriter 基本用法,创建 xlsx 文件并添加数据 官方文档:http://xlsxwriter.readthedocs.org/ xlsxwriter 可以操作 xls...

解决Python selenium get页面很慢时的问题

解决Python selenium get页面很慢时的问题

driver.get("url")等到页面全部加载渲染完成后才会执行后续的脚本。 在执行脚本时,driver.get("url") ,如果当前的url页面内容较多加载特别慢,很费时间,但...

Python中顺序表原理与实现方法详解

本文实例讲述了Python中顺序表原理与实现方法。分享给大家供大家参考,具体如下: Python中的顺序表 Python中的list和tuple两种类型采用了顺序表的实现技术,具有顺序表...

python中计算一个列表中连续相同的元素个数方法

最简单的例子: a = [1,1,1,1,2,2,2,3,3,1,1,1,3] # 问:计算a中最多有几个连续的1 很明显,答案是4 如果用代码实现,最先想到的就是itertool...