code: def echo_to_output_stream(self, email_messages):
          
          if not email_messages:
              return
          with self._lock:
              try:
                  stream_created = self.open()
                  for message in email_messages:
                      self.write_to_stream(message)
                      self.stream.flush()  # flush after each message
                  if stream_created:
                      self.close()
              except Exception:
                  if not self.fail_silently:
                      raise
docs: Write all messages to the stream in a thread-safe way.