Ref : https://github.com/python/cpython/pull/25174

https://github.com/localstack/localstack/blob/5a5edcd9d9ccbd6a56f23d71e3a1131b8c814996/localstack/utils/common.py#L324
ident = self._ident()
        removed = self.CONTEXTS_BY_THREAD.pop(ident, None)
        if not self.CONTEXTS_BY_THREAD:
            
            self._set(self.orig_stdout, self.orig_stderr, self.orig___stdout, self.orig___stderr)
        
        removed._stdout.flush()
        removed._stderr.flush()
        out = removed._stdout.getvalue()
        err = removed._stderr.getvalue()
        
        removed._stdout.close()
        removed._stderr.close()
        removed._stdout = out
        removed._stderr = err
    def _set(self, out, err, __out, __err):
        sys.stdout, sys.stderr, sys.__stdout__, sys.__stderr__ = (out, err, __out, __err)
    def _proxy(self, original_stream, type):
        def proxy():
            ident = self._ident()
            ctx = self.CONTEXTS_BY_THREAD.get(ident)
            if ctx:
                return ctx._stdout if type == 'stdout' else ctx._stderr
            return original_stream
        return proxy
    def _ident(self):
        return threading.currentThread().ident
    def stdout(self):