forked from boocmp/go-stt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
320 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ asyncio | |
pydantic | ||
pydantic-settings | ||
six | ||
msgspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ runners: | |
timeout: 900 | ||
resources: | ||
cpu: 4 | ||
nvidia.com/gpu: 1 | ||
nvidia.com/gpu: 1 | ||
workers_per_resource: 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ asyncio | |
pydantic | ||
pydantic-settings | ||
six | ||
msgspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from . import messages | ||
from . import client | ||
from . import server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import asyncio | ||
|
||
if __name__=='__main__': | ||
import messages | ||
else: | ||
from . import messages | ||
|
||
HOST, PORT = "localhost", 3015 | ||
|
||
class Publisher: | ||
def __init__(self, pair): | ||
self._pair: str = pair | ||
self._reader: asyncio.StreamReader = None | ||
self._writer: asyncio.StreamReader = None | ||
|
||
def __del__(self): | ||
if self._writer: | ||
self._writer.close() | ||
|
||
async def __aenter__(self): | ||
await self._open(self._pair) | ||
return self | ||
|
||
async def __aexit__(self, *_): | ||
if self._writer: | ||
self._writer.close() | ||
|
||
async def push(self, r: messages.Text): | ||
await messages.send_request(self._writer, r) | ||
|
||
async def _open(self, pair: str, timeout: float = 10.0): | ||
async def op() : | ||
self._reader, self._writer = await asyncio.open_connection(HOST, PORT) | ||
await messages.send_request(self._writer, messages.Publish(pair)) | ||
ready = await messages.receive_request(self._reader) | ||
if not isinstance(ready, messages.Ready): | ||
raise asyncio.InvalidStateError() | ||
|
||
await asyncio.wait_for(op(), timeout) | ||
|
||
|
||
class Subscriber: | ||
def __init__(self, pair: str): | ||
self._pair: str = pair | ||
self._reader: asyncio.StreamReader = None | ||
self._writer: asyncio.StreamReader = None | ||
|
||
def __del__(self): | ||
if self._writer: | ||
self._writer.close() | ||
|
||
async def __aenter__(self): | ||
await self._open(self._pair) | ||
return self | ||
|
||
async def __aexit__(self, *_): | ||
if self._writer: | ||
self._writer.close() | ||
|
||
async def pull(self) -> messages.Text: | ||
try: | ||
r = await messages.receive_request(self._reader) | ||
except asyncio.IncompleteReadError: | ||
return None | ||
|
||
if not isinstance(r, messages.Text): | ||
raise asyncio.InvalidStateError() | ||
return r | ||
|
||
async def _open(self, pair: str, timeout: float = 10.0): | ||
async def op() : | ||
self._reader, self._writer = await asyncio.open_connection(HOST, PORT) | ||
await messages.send_request(self._writer, messages.Subscribe(pair)) | ||
ready = await messages.receive_request(self._reader) | ||
if not isinstance(ready, messages.Ready): | ||
raise asyncio.InvalidStateError() | ||
|
||
await asyncio.wait_for(op(), timeout) | ||
|
||
if __name__ == '__main__': | ||
async def publisher(pair): | ||
try: | ||
async def op(): | ||
async with Publisher(pair) as pub: | ||
for i in range(0, 30): | ||
await pub.push(messages.Text(f"{pair} -> {i}", False)) | ||
await asyncio.sleep(1) | ||
await asyncio.wait_for(op(), 3) | ||
except Exception as e: | ||
print(e) | ||
pass | ||
|
||
async def subscriber(pair): | ||
|
||
try: | ||
async with Subscriber(pair) as sub: | ||
while True: | ||
r = await sub.pull() | ||
if r is None: | ||
break | ||
print(r) | ||
|
||
except asyncio.IncompleteReadError: | ||
pass | ||
except Exception as e: | ||
print(e) | ||
pass | ||
|
||
async def batch(pair): | ||
try: | ||
await asyncio.gather(subscriber(pair), publisher(pair)) | ||
except Exception as e: | ||
print(e) | ||
pass | ||
|
||
async def main(): | ||
tasks = [] | ||
for i in range(20): | ||
tasks.append(asyncio.create_task(batch(str(i)))) | ||
|
||
for t in tasks: | ||
await t | ||
|
||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import asyncio | ||
import msgspec | ||
|
||
class Publish(msgspec.Struct, tag = True): | ||
pair: str | ||
|
||
class Subscribe(msgspec.Struct, tag = True): | ||
pair: str | ||
|
||
class Ready(msgspec.Struct, tag = True): | ||
pass | ||
|
||
class Text(msgspec.Struct, tag = True): | ||
text: str | ||
final: bool | ||
|
||
Request = Publish | Subscribe | Ready | Text | ||
|
||
RequestEncoder = msgspec.msgpack.Encoder() | ||
RequestDecoder = msgspec.msgpack.Decoder(Request) | ||
|
||
async def receive_request(reader: asyncio.StreamReader) -> Request: | ||
len = int.from_bytes(await reader.readexactly(4), "big") | ||
if len < 0 or len > 1024: | ||
raise asyncio.InvalidStateError() | ||
data = await reader.readexactly(len) | ||
return RequestDecoder.decode(data) | ||
|
||
async def send_request(writer, req): | ||
data = RequestEncoder.encode(req) | ||
writer.write(len(data).to_bytes(4, "big")) | ||
writer.write(data) | ||
await writer.drain() |
Oops, something went wrong.