Call the flush() method on sys.stdout and then on sys.stderr. It can be used to make sure that the logs order is consistent before writing into stderr.
test.support/flush_std_streams
flush_std_streams