@@ -286,18 +286,20 @@ def __init__(
286286 attributes = dict (self ._header .attributes ),
287287 attachments = list (self ._header .text_header .attached_stream_ids ),
288288 )
289+ self ._write_lock = asyncio .Lock ()
289290
290291 async def write (self , text : str ):
291- for chunk in split_utf8 (text , STREAM_CHUNK_SIZE ):
292- content = chunk .encode ()
293- chunk_index = self ._next_chunk_index
294- self ._next_chunk_index += 1
295- chunk_msg = proto_DataStream .Chunk (
296- stream_id = self ._header .stream_id ,
297- chunk_index = chunk_index ,
298- content = content ,
299- )
300- await self ._send_chunk (chunk_msg )
292+ async with self ._write_lock :
293+ for chunk in split_utf8 (text , STREAM_CHUNK_SIZE ):
294+ content = chunk .encode ()
295+ chunk_index = self ._next_chunk_index
296+ self ._next_chunk_index += 1
297+ chunk_msg = proto_DataStream .Chunk (
298+ stream_id = self ._header .stream_id ,
299+ chunk_index = chunk_index ,
300+ content = content ,
301+ )
302+ await self ._send_chunk (chunk_msg )
301303
302304 @property
303305 def info (self ) -> TextStreamInfo :
0 commit comments