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!