def send_messages(self, messages):
if not messages:<EOL><INDENT>return<EOL><DEDENT>self._lock.acquire()<EOL>try:<EOL><INDENT>try:<EOL><INDENT>stream_created = self.open()<EOL>for message in messages:<EOL><INDENT>self.stream.write(render_message(message))<EOL>self.stream.write('<STR_LIT:\n>')<EOL>self.stream.write('<STR_LIT:->'*<NUM_LIT>)<EOL>self.stream.write('<STR_LIT:\n>')<EOL>self.stream.flush()  <EOL><DEDENT>if stream_created:<EOL><INDENT>self.close()<EOL><DEDENT><DEDENT>except:<EOL><INDENT>if not self.fail_silently:<EOL><INDENT>raise<EOL><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>self._lock.release()<EOL><DEDENT>return len(messages)<EOL>
Write all messages to the stream in a thread-safe way.