Python programming test computer to enable the maximum number of threads instance code, python instance

Source: Internet
Author: User

Python programming test computer to enable the maximum number of threads instance code, python instance

The example code in this article mainly implements the maximum number of threads enabled by the python programming test computer. The specific implementation code is as follows.

#! /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 = 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. a Cquire () # 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 failed t BaseException, e: print "exception:", type (e), e time. sleep (5) print "maxium I =", I finally: cond. acquire () cond. policyall () cond. release () time. sleep (3) print threading. currentThread (). getName (), "quit"

Running result section:

Summary

The above is all about the code for enabling the maximum number of threads on the python programming and testing computer. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.