<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , unicode_literals <EOL> import os <EOL> from collections import deque <EOL> from . import exceptions <EOL> from . five import Empty , LifoQueue as _LifoQueue <EOL> from . utils import register_after_fork <EOL> from . utils . functional import lazy <EOL> def _after_fork_cleanup_resource ( resource ) : <EOL> try : <EOL> resource . force_close_all ( ) <EOL> except Exception : <EOL> pass <EOL> class LifoQueue ( _LifoQueue ) : <EOL> def _init ( self , maxsize ) : <EOL> self . queue = deque ( ) <EOL> class Resource ( object ) : <EOL> LimitExceeded = exceptions . LimitExceeded <EOL> close_after_fork = False <EOL> def __init__ ( self , limit = None , preload = None , close_after_fork = None ) : <EOL> self . _limit = limit <EOL> self . preload = preload or <NUM_LIT:0> <EOL> self . _closed = False <EOL> self . close_after_fork = ( <EOL> close_after_fork <EOL> if close_after_fork is not None else self . close_after_fork <EOL> ) <EOL> self . _resource = LifoQueue ( ) <EOL> self . _dirty = set ( ) <EOL> if self . close_after_fork and register_after_fork is not None : <EOL> register_after_fork ( self , _after_fork_cleanup_resource ) <EOL> self . setup ( ) <EOL> def setup ( self ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def _add_when_empty ( self ) : <EOL> if self . limit and len ( self . _dirty ) >= self . limit : <EOL> raise self . LimitExceeded ( self . limit ) <EOL> self . _resource . put_nowait ( self . new ( ) ) <EOL> def acquire ( self , block = False , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _closed : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> if self . limit : <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> R = self . _resource . get ( block = block , timeout = timeout ) <EOL> except Empty : <EOL> self . _add_when_empty ( ) <EOL> else : <EOL> try : <EOL> R = self . prepare ( R ) <EOL> except BaseException : <EOL> if isinstance ( R , lazy ) : <EOL> self . _resource . put_nowait ( R ) <EOL> else : <EOL> self . release ( R ) <EOL> raise <EOL> self . _dirty . add ( R ) <EOL> break <EOL> else : <EOL> R = self . prepare ( self . new ( ) ) <EOL> def release ( ) : <EOL> """<STR_LIT>""" <EOL> self . release ( R ) <EOL> R . release = release <EOL> return R <EOL> def prepare ( self , resource ) : <EOL> return resource <EOL> def close_resource ( self , resource ) : <EOL> resource . close ( ) <EOL> def release_resource ( self , resource ) : <EOL> pass <EOL> def replace ( self , resource ) : <EOL> """<STR_LIT>""" <EOL> if self . limit : <EOL> self . _dirty . discard ( resource ) <EOL> self . close_resource ( resource ) <EOL> def release ( self , resource ) : <EOL> if self . limit : <EOL> self . _dirty . discard ( resource ) <EOL> self . _resource . put_nowait ( resource ) <EOL> self . release_resource ( resource ) <EOL> else : <EOL> self . close_resource ( resource ) <EOL> def collect_resource ( self , resource ) : <EOL> pass <EOL> def force_close_all ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _closed : <EOL> return <EOL> self . _closed = True <EOL> dirty = self . _dirty <EOL> resource = self . _resource <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> dres = dirty . pop ( ) <EOL> except KeyError : <EOL> break <EOL> try : <EOL> self . collect_resource ( dres ) <EOL> except AttributeError : <EOL> pass <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> res = resource . queue . pop ( ) <EOL> except IndexError : <EOL> break <EOL> try : <EOL> self . collect_resource ( res ) <EOL> except AttributeError : <EOL> pass <EOL> def resize ( self , limit , force = False , ignore_errors = False , reset = False ) : <EOL> prev_limit = self . _limit <EOL> if ( self . _dirty and limit < self . _limit ) and not ignore_errors : <EOL> if not force : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" . format ( <EOL> limit , self . _limit ) ) <EOL> reset = True <EOL> self . _limit = limit <EOL> if reset : <EOL> try : <EOL> self . force_close_all ( ) <EOL> except Exception : <EOL> pass <EOL> self . setup ( ) <EOL> if limit < prev_limit : <EOL> self . _shrink_down ( ) <EOL> def _shrink_down ( self ) : <EOL> resource = self . _resource <EOL> with resource . mutex : <EOL> while len ( resource . queue ) > self . limit : <EOL> self . collect_resource ( resource . queue . popleft ( ) ) <EOL> @ property <EOL> def limit ( self ) : <EOL> return self . _limit <EOL> @ limit . setter <EOL> def limit ( self , limit ) : <EOL> self . resize ( limit ) <EOL> if os . environ . get ( '<STR_LIT>' ) : <EOL> _orig_acquire = acquire <EOL> _orig_release = release <EOL> _next_resource_id = <NUM_LIT:0> <EOL> def acquire ( self , * args , ** kwargs ) : <EOL> import traceback <EOL> id = self . _next_resource_id = self . _next_resource_id + <NUM_LIT:1> <EOL> print ( '<STR_LIT>' . format ( id , self . __class__ . __name__ ) ) <EOL> r = self . _orig_acquire ( * args , ** kwargs ) <EOL> r . _resource_id = id <EOL> print ( '<STR_LIT>' . format ( id , self . __class__ . __name__ ) ) <EOL> if not hasattr ( r , '<STR_LIT>' ) : <EOL> r . acquired_by = [ ] <EOL> r . acquired_by . append ( traceback . format_stack ( ) ) <EOL> return r <EOL> def release ( self , resource ) : <EOL> id = resource . _resource_id <EOL> print ( '<STR_LIT>' . format ( id , self . __class__ . __name__ ) ) <EOL> r = self . _orig_release ( resource ) <EOL> print ( '<STR_LIT>' . format ( id , self . __class__ . __name__ ) ) <EOL> self . _next_resource_id -= <NUM_LIT:1> <EOL> return r </s>
