mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-16 16:51:02 +02:00
Add ID field and run response in thread, FIXME: await
This commit is contained in:
parent
f34a52e1c1
commit
f61635384e
1 changed files with 32 additions and 3 deletions
|
|
@ -27,7 +27,7 @@ class CommandEndpoint(SocketEndpoint):
|
|||
async def start(self):
|
||||
pass
|
||||
|
||||
async def async_thread(self, ws, running):
|
||||
async def ASDasync_thread(self, ws, running):
|
||||
|
||||
while running.get():
|
||||
|
||||
|
|
@ -54,6 +54,26 @@ class CommandEndpoint(SocketEndpoint):
|
|||
|
||||
running.stop()
|
||||
|
||||
async def processor(self, ws, id, service, request):
|
||||
|
||||
try:
|
||||
|
||||
print(id, service, request)
|
||||
|
||||
requestor = self.services[service]
|
||||
|
||||
resp = await requestor.process(request)
|
||||
|
||||
await ws.send_json({"id": id, "response": resp })
|
||||
|
||||
except Exception as e:
|
||||
|
||||
await ws.send_json({"id": id, "error": str(e)})
|
||||
|
||||
async def async_thread(self, ws, running):
|
||||
while running.get():
|
||||
await asyncio.sleep(1)
|
||||
|
||||
async def listener(self, ws, running):
|
||||
|
||||
async for msg in ws:
|
||||
|
|
@ -64,13 +84,22 @@ class CommandEndpoint(SocketEndpoint):
|
|||
else:
|
||||
|
||||
try:
|
||||
|
||||
data = msg.json()
|
||||
|
||||
if data["service"] not in self.services:
|
||||
raise RuntimeError("Bad service")
|
||||
|
||||
await self.q.put(
|
||||
( data["service"], data["request"] )
|
||||
if "request" not in data:
|
||||
raise RuntimeError("Bad message")
|
||||
|
||||
if "id" not in data:
|
||||
raise RuntimeError("Bad message")
|
||||
|
||||
asyncio.create_task(
|
||||
self.processor(
|
||||
ws, data["id"], data["service"], data["request"]
|
||||
)
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue