<s> from __future__ import print_function <EOL> import collections <EOL> from contextlib import contextmanager <EOL> from eventlet import queue <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class Pool ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , min_size = <NUM_LIT:0> , max_size = <NUM_LIT:4> , order_as_stack = False , create = None ) : <EOL> """<STR_LIT>""" <EOL> self . min_size = min_size <EOL> self . max_size = max_size <EOL> self . order_as_stack = order_as_stack <EOL> self . current_size = <NUM_LIT:0> <EOL> self . channel = queue . LightQueue ( <NUM_LIT:0> ) <EOL> self . free_items = collections . deque ( ) <EOL> if create is not None : <EOL> self . create = create <EOL> for x in range ( min_size ) : <EOL> self . current_size += <NUM_LIT:1> <EOL> self . free_items . append ( self . create ( ) ) <EOL> def get ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . free_items : <EOL> return self . free_items . popleft ( ) <EOL> self . current_size += <NUM_LIT:1> <EOL> if self . current_size <= self . max_size : <EOL> try : <EOL> created = self . create ( ) <EOL> except : <EOL> self . current_size -= <NUM_LIT:1> <EOL> raise <EOL> return created <EOL> self . current_size -= <NUM_LIT:1> <EOL> return self . channel . get ( ) <EOL> @ contextmanager <EOL> def item ( self ) : <EOL> """<STR_LIT>""" <EOL> obj = self . get ( ) <EOL> try : <EOL> yield obj <EOL> finally : <EOL> self . put ( obj ) <EOL> def put ( self , item ) : <EOL> """<STR_LIT>""" <EOL> if self . current_size > self . max_size : <EOL> self . current_size -= <NUM_LIT:1> <EOL> return <EOL> if self . waiting ( ) : <EOL> self . channel . put ( item ) <EOL> else : <EOL> if self . order_as_stack : <EOL> self . free_items . appendleft ( item ) <EOL> else : <EOL> self . free_items . append ( item ) <EOL> def resize ( self , new_size ) : <EOL> """<STR_LIT>""" <EOL> self . max_size = new_size <EOL> def free ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( self . free_items ) + self . max_size - self . current_size <EOL> def waiting ( self ) : <EOL> """<STR_LIT>""" <EOL> return max ( <NUM_LIT:0> , self . channel . getting ( ) - self . channel . putting ( ) ) <EOL> def create ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> class Token ( object ) : <EOL> pass <EOL> class TokenPool ( Pool ) : <EOL> """<STR_LIT>""" <EOL> def create ( self ) : <EOL> return Token ( ) </s>
