Skip to content

Commit

Permalink
add write lock
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasIO committed Feb 14, 2025
1 parent 18ed02a commit 1962bd5
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions livekit-rtc/livekit/rtc/data_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,18 +286,20 @@ def __init__(
attributes=dict(self._header.attributes),
attachments=list(self._header.text_header.attached_stream_ids),
)
self._write_lock = asyncio.Lock()

async def write(self, text: str):
for chunk in split_utf8(text, STREAM_CHUNK_SIZE):
content = chunk.encode()
chunk_index = self._next_chunk_index
self._next_chunk_index += 1
chunk_msg = proto_DataStream.Chunk(
stream_id=self._header.stream_id,
chunk_index=chunk_index,
content=content,
)
await self._send_chunk(chunk_msg)
async with self._write_lock:
for chunk in split_utf8(text, STREAM_CHUNK_SIZE):
content = chunk.encode()
chunk_index = self._next_chunk_index
self._next_chunk_index += 1
chunk_msg = proto_DataStream.Chunk(
stream_id=self._header.stream_id,
chunk_index=chunk_index,
content=content,
)
await self._send_chunk(chunk_msg)

@property
def info(self) -> TextStreamInfo:
Expand Down

0 comments on commit 1962bd5

Please sign in to comment.