<s> from eventlet import coros , proc , api <EOL> from eventlet . semaphore import Semaphore <EOL> import warnings <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> class Pool ( object ) : <EOL> def __init__ ( self , min_size = <NUM_LIT:0> , max_size = <NUM_LIT:4> , track_events = False ) : <EOL> if min_size > max_size : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . max_size = max_size <EOL> self . sem = Semaphore ( max_size ) <EOL> self . procs = proc . RunningProcSet ( ) <EOL> if track_events : <EOL> self . results = coros . queue ( ) <EOL> else : <EOL> self . results = None <EOL> def resize ( self , new_max_size ) : <EOL> """<STR_LIT>""" <EOL> max_size_delta = new_max_size - self . max_size <EOL> self . sem . counter += max_size_delta <EOL> self . max_size = new_max_size <EOL> @ property <EOL> def current_size ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . procs ) <EOL> def free ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . sem . counter <EOL> def execute ( self , func , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . sem . locked ( ) and api . getcurrent ( ) in self . procs : <EOL> p = proc . spawn ( func , * args , ** kwargs ) <EOL> try : <EOL> p . wait ( ) <EOL> except : <EOL> pass <EOL> else : <EOL> self . sem . acquire ( ) <EOL> p = self . procs . spawn ( func , * args , ** kwargs ) <EOL> p . link ( lambda p : self . sem . release ( ) ) <EOL> if self . results is not None : <EOL> p . link ( self . results ) <EOL> return p <EOL> execute_async = execute <EOL> def _execute ( self , evt , func , args , kw ) : <EOL> p = self . execute ( func , * args , ** kw ) <EOL> p . link ( evt ) <EOL> return p <EOL> def waitall ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . procs . waitall ( ) <EOL> wait_all = waitall <EOL> def wait ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . results . wait ( ) <EOL> def waiting ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . sem . balance < <NUM_LIT:0> : <EOL> return - self . sem . balance <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def killall ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . procs . killall ( ) <EOL> def launch_all ( self , function , iterable ) : <EOL> """<STR_LIT>""" <EOL> for tup in iterable : <EOL> self . execute ( function , * tup ) <EOL> def process_all ( self , function , iterable ) : <EOL> """<STR_LIT>""" <EOL> self . launch_all ( function , iterable ) <EOL> self . wait_all ( ) <EOL> def generate_results ( self , function , iterable , qsize = None ) : <EOL> """<STR_LIT>""" <EOL> tuples = iter ( enumerate ( iterable ) ) <EOL> try : <EOL> index , args = tuples . next ( ) <EOL> except StopIteration : <EOL> return <EOL> q = coros . queue ( max_size = qsize ) <EOL> finished = <NUM_LIT:0> <EOL> try : <EOL> while True : <EOL> while self . free ( ) > <NUM_LIT:0> or finished == index : <EOL> self . _execute ( q , function , args , { } ) <EOL> index , args = tuples . next ( ) <EOL> yield q . wait ( ) <EOL> finished += <NUM_LIT:1> <EOL> except StopIteration : <EOL> pass <EOL> while finished < index + <NUM_LIT:1> : <EOL> yield q . wait ( ) <EOL> finished += <NUM_LIT:1> </s>
