Skip to content

Commit

Permalink
optimize import and cleanup_modules
Browse files Browse the repository at this point in the history
  • Loading branch information
nggit committed Dec 1, 2024
1 parent a41e330 commit b839047
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions httpout/httpout.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def wait(coro, timeout=None):
return asyncio.run_coroutine_threadsafe(coro, loop).result(timeout)

def load_module(name, globals, level=0):
if globals['__name__'] == '__globals__':
modules = worker['modules']
else:
if '__server__' in globals:
modules = globals['__main__'].__server__['modules']
else:
modules = worker['modules']

if name in modules:
# already imported
Expand All @@ -68,7 +68,7 @@ def load_module(name, globals, level=0):
if module:
logger.info('%s: importing %s', globals['__name__'], name)

if globals['__name__'] != '__globals__':
if '__server__' in globals:
module.__main__ = globals['__main__']
module.__server__ = globals['__main__'].__server__
module.print = globals['__main__'].print
Expand Down Expand Up @@ -114,12 +114,12 @@ def ho_import(name, globals=None, locals=None, fromlist=(), level=0):
return module

if name == 'httpout' or name.startswith('httpout.'):
if globals['__name__'] == '__globals__':
module = worker['modules'][globals['__name__']]
else:
if '__server__' in globals:
module = globals['__main__'].__server__['modules'][
globals['__name__']
]
else:
module = worker['modules'][globals['__name__']]

# handles virtual imports,
# e.g. from httpout import request, response
Expand All @@ -128,14 +128,14 @@ def ho_import(name, globals=None, locals=None, fromlist=(), level=0):
if child in module.__dict__:
continue

if (globals['__name__'] != '__globals__' and
if ('__server__' in globals and
child in module.__server__):
module.__dict__[child] = module.__server__[
child
]
elif child in worker and (
child != 'app' or
globals['__name__'] == '__globals__'):
'__server__' not in globals):
module.__dict__[child] = worker[child]
else:
raise ImportError(
Expand Down Expand Up @@ -223,6 +223,21 @@ async def _on_request(self, **server):

server['request'] = HTTPRequest(request, server)
server['response'] = HTTPResponse(response)

if (request.protocol.options['ws'] and
b'upgrade' in request.headers and
b'connection' in request.headers and
b'sec-websocket-key' in request.headers and
request.headers[b'upgrade'].lower() == b'websocket'):
server['websocket'] = WebSocket(request, response)
else:
server['websocket'] = None

excludes = (server['response'].print,
server['response'].run_coroutine,
g.wait,
*server.values())

server['REQUEST_METHOD'] = request.method.decode('latin-1')
server['SCRIPT_NAME'] = module_path[len(document_root):].replace(
os.sep, '/'
Expand All @@ -235,15 +250,6 @@ async def _on_request(self, **server):
server['REQUEST_SCHEME'] = request.scheme.decode('latin-1')
server['DOCUMENT_ROOT'] = document_root

if (request.protocol.options['ws'] and
b'upgrade' in request.headers and
b'connection' in request.headers and
b'sec-websocket-key' in request.headers and
request.headers[b'upgrade'].lower() == b'websocket'):
server['websocket'] = WebSocket(request, response)
else:
server['websocket'] = None

module = ModuleType('__main__')
module.__file__ = module_path
module.__main__ = module
Expand Down Expand Up @@ -299,12 +305,7 @@ async def _on_request(self, **server):
request.protocol.print_exception(exc)
finally:
await g.executor.submit(
cleanup_modules, server['modules'], (module.print,
module.run,
module.wait,
g,
ctx,
server['response'])
cleanup_modules, server['modules'], excludes
)
await server['response'].join()
server['modules'].clear()
Expand Down

0 comments on commit b839047

Please sign in to comment.