@ -155,8 +155,8 @@ def get_sys_executable():
return ' /usr/bin/python '
return ' /usr/bin/python '
_core_source_cache = None
_core_source_lock = threading . Lock ( )
_core_source_lock = threading . Lock ( )
_core_source_partial = None
def _get_core_source ( ) :
def _get_core_source ( ) :
@ -168,23 +168,25 @@ def _get_core_source():
return inspect . getsource ( mitogen . core )
return inspect . getsource ( mitogen . core )
def get_core_source ( ) :
def get_core_source _partial ( ) :
"""
"""
_get_core_source ( ) is expensive , even with @lru_cache in minify . py , threads
_get_core_source ( ) is expensive , even with @lru_cache in minify . py , threads
can enter it simultaneously causing severe slowdowns .
can enter it simultaneously causing severe slowdowns .
"""
"""
global _core_source_cache
global _core_source_partial
if _core_source_cache is not None :
return _core_source_cache
if _core_source_partial is None :
_core_source_lock . acquire ( )
_core_source_lock . acquire ( )
try :
try :
if _core_source_cache is None :
if _core_source_partial is None :
_core_source_cache = _get_core_source ( )
_core_source_partial = PartialZlib (
return _core_source_cache
_get_core_source ( ) . encode ( ' utf-8 ' )
)
finally :
finally :
_core_source_lock . release ( )
_core_source_lock . release ( )
return _core_source_partial
def get_default_remote_name ( ) :
def get_default_remote_name ( ) :
"""
"""
@ -572,6 +574,26 @@ def write_all(fd, s, deadline=None):
poller . close ( )
poller . close ( )
class PartialZlib ( object ) :
def __init__ ( self , s ) :
self . s = s
if sys . version_info > ( 2 , 5 ) :
self . _compressor = zlib . compressobj ( 9 )
self . _out = self . _compressor . compress ( s )
self . _out + = self . _compressor . flush ( zlib . Z_SYNC_FLUSH )
else :
self . _compressor = None
def append ( self , s ) :
if self . _compressor is None :
return zlib . compress ( self . s + s , 9 )
else :
compressor = self . _compressor . copy ( )
out = self . _out
out + = compressor . compress ( s )
return out + compressor . flush ( )
class IteratingRead ( object ) :
class IteratingRead ( object ) :
def __init__ ( self , fds , deadline = None ) :
def __init__ ( self , fds , deadline = None ) :
self . deadline = deadline
self . deadline = deadline
@ -1300,11 +1322,12 @@ class Stream(mitogen.core.Stream):
}
}
def get_preamble ( self ) :
def get_preamble ( self ) :
s ource = get_core_source ( )
s uffix = (
source + = ' \n ExternalContext( %r ).main() \n ' % (
' \n ExternalContext( %r ).main() \n ' % \
self . get_econtext_config ( ) ,
( self . get_econtext_config ( ) , )
)
)
return zlib . compress ( source . encode ( ' utf-8 ' ) , 9 )
partial = get_core_source_partial ( )
return partial . append ( suffix . encode ( ' utf-8 ' ) )
def start_child ( self ) :
def start_child ( self ) :
args = self . get_boot_command ( )
args = self . get_boot_command ( )