codekingpro/portable-devtools
115k
1"""2This module provides a @concurrent decorator primitive to3offload computations from mitmproxy's main master thread.4"""5 6import asyncio7import inspect8 9from mitmproxy import hooks10 11 12def concurrent(fn):13 if fn.__name__ not in set(hooks.all_hooks.keys()) - {"load", "configure"}:14 raise NotImplementedError(15 "Concurrent decorator not supported for '%s' method." % fn.__name__16 )17 18 async def _concurrent(*args):19 def run():20 if inspect.iscoroutinefunction(fn):21 # Run the async function in a new event loop22 loop = asyncio.new_event_loop()23 try:24 loop.run_until_complete(fn(*args))25 finally:26 loop.close()27 else:28 fn(*args)29 30 await asyncio.get_running_loop().run_in_executor(None, run)31 32 return _concurrent33 