<s> import traceback <EOL> from eventlet import event <EOL> from eventlet import greenthread <EOL> from eventlet import queue <EOL> from eventlet import semaphore <EOL> from eventlet . support import greenlets as greenlet <EOL> from eventlet . support import six <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> DEBUG = True <EOL> class GreenPool ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , size = <NUM_LIT:1000> ) : <EOL> self . size = size <EOL> self . coroutines_running = set ( ) <EOL> self . sem = semaphore . Semaphore ( size ) <EOL> self . no_coros_running = event . Event ( ) <EOL> def resize ( self , new_size ) : <EOL> """<STR_LIT>""" <EOL> size_delta = new_size - self . size <EOL> self . sem . counter += size_delta <EOL> self . size = new_size <EOL> def running ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . coroutines_running ) <EOL> def free ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . sem . counter <EOL> def spawn ( self , function , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> current = greenthread . getcurrent ( ) <EOL> if self . sem . locked ( ) and current in self . coroutines_running : <EOL> gt = greenthread . GreenThread ( current ) <EOL> gt . main ( function , args , kwargs ) <EOL> return gt <EOL> else : <EOL> self . sem . acquire ( ) <EOL> gt = greenthread . spawn ( function , * args , ** kwargs ) <EOL> if not self . coroutines_running : <EOL> self . no_coros_running = event . Event ( ) <EOL> self . coroutines_running . add ( gt ) <EOL> gt . link ( self . _spawn_done ) <EOL> return gt <EOL> def _spawn_n_impl ( self , func , args , kwargs , coro ) : <EOL> try : <EOL> try : <EOL> func ( * args , ** kwargs ) <EOL> except ( KeyboardInterrupt , SystemExit , greenlet . GreenletExit ) : <EOL> raise <EOL> except : <EOL> if DEBUG : <EOL> traceback . print_exc ( ) <EOL> finally : <EOL> if coro is None : <EOL> return <EOL> else : <EOL> coro = greenthread . getcurrent ( ) <EOL> self . _spawn_done ( coro ) <EOL> def spawn_n ( self , function , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> current = greenthread . getcurrent ( ) <EOL> if self . sem . locked ( ) and current in self . coroutines_running : <EOL> self . _spawn_n_impl ( function , args , kwargs , None ) <EOL> else : <EOL> self . sem . acquire ( ) <EOL> g = greenthread . spawn_n ( <EOL> self . _spawn_n_impl , <EOL> function , args , kwargs , True ) <EOL> if not self . coroutines_running : <EOL> self . no_coros_running = event . Event ( ) <EOL> self . coroutines_running . add ( g ) <EOL> def waitall ( self ) : <EOL> """<STR_LIT>""" <EOL> assert greenthread . getcurrent ( ) not in self . coroutines_running , "<STR_LIT>" "<STR_LIT>" <EOL> if self . running ( ) : <EOL> self . no_coros_running . wait ( ) <EOL> def _spawn_done ( self , coro ) : <EOL> self . sem . release ( ) <EOL> if coro is not None : <EOL> self . coroutines_running . remove ( coro ) <EOL> if self . sem . balance == self . size : <EOL> self . no_coros_running . send ( None ) <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 _do_map ( self , func , it , gi ) : <EOL> for args in it : <EOL> gi . spawn ( func , * args ) <EOL> gi . spawn ( return_stop_iteration ) <EOL> def starmap ( self , function , iterable ) : <EOL> """<STR_LIT>""" <EOL> if function is None : <EOL> function = lambda * a : a <EOL> gi = GreenMap ( self . size ) <EOL> greenthread . spawn_n ( self . _do_map , function , iterable , gi ) <EOL> return gi <EOL> def imap ( self , function , * iterables ) : <EOL> """<STR_LIT>""" <EOL> return self . starmap ( function , six . moves . zip ( * iterables ) ) <EOL> def return_stop_iteration ( ) : <EOL> return StopIteration ( ) <EOL> class GreenPile ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , size_or_pool = <NUM_LIT:1000> ) : <EOL> if isinstance ( size_or_pool , GreenPool ) : <EOL> self . pool = size_or_pool <EOL> else : <EOL> self . pool = GreenPool ( size_or_pool ) <EOL> self . waiters = queue . LightQueue ( ) <EOL> self . used = False <EOL> self . counter = <NUM_LIT:0> <EOL> def spawn ( self , func , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> self . used = True <EOL> self . counter += <NUM_LIT:1> <EOL> try : <EOL> gt = self . pool . spawn ( func , * args , ** kw ) <EOL> self . waiters . put ( gt ) <EOL> except : <EOL> self . counter -= <NUM_LIT:1> <EOL> raise <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . counter == <NUM_LIT:0> and self . used : <EOL> raise StopIteration ( ) <EOL> try : <EOL> return self . waiters . get ( ) . wait ( ) <EOL> finally : <EOL> self . counter -= <NUM_LIT:1> <EOL> __next__ = next <EOL> class GreenMap ( GreenPile ) : <EOL> def __init__ ( self , size_or_pool ) : <EOL> super ( GreenMap , self ) . __init__ ( size_or_pool ) <EOL> self . waiters = queue . LightQueue ( maxsize = self . pool . size ) <EOL> def next ( self ) : <EOL> try : <EOL> val = self . waiters . get ( ) . wait ( ) <EOL> if isinstance ( val , StopIteration ) : <EOL> raise val <EOL> else : <EOL> return val <EOL> finally : <EOL> self . counter -= <NUM_LIT:1> <EOL> __next__ = next </s>
