mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-05 19:32:11 +02:00
Fix loop logic flaws in loader (#203)
This commit is contained in:
parent
803f110891
commit
61031270e4
1 changed files with 68 additions and 14 deletions
|
|
@ -19,8 +19,13 @@ class Running:
|
||||||
def get(self): return self.running
|
def get(self): return self.running
|
||||||
def stop(self): self.running = False
|
def stop(self): self.running = False
|
||||||
|
|
||||||
|
ge_counts = 0
|
||||||
|
t_counts = 0
|
||||||
|
|
||||||
async def load_ge(running, queue, url):
|
async def load_ge(running, queue, url):
|
||||||
|
|
||||||
|
global ge_counts
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
|
|
||||||
async with session.ws_connect(f"{url}load/graph-embeddings") as ws:
|
async with session.ws_connect(f"{url}load/graph-embeddings") as ws:
|
||||||
|
|
@ -29,6 +34,11 @@ async def load_ge(running, queue, url):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
msg = await asyncio.wait_for(queue.get(), 1)
|
msg = await asyncio.wait_for(queue.get(), 1)
|
||||||
|
|
||||||
|
# End of load
|
||||||
|
if msg is None:
|
||||||
|
break
|
||||||
|
|
||||||
except:
|
except:
|
||||||
# Hopefully it's TimeoutError. Annoying to match since
|
# Hopefully it's TimeoutError. Annoying to match since
|
||||||
# it changed in 3.11.
|
# it changed in 3.11.
|
||||||
|
|
@ -45,10 +55,17 @@ async def load_ge(running, queue, url):
|
||||||
"entity": msg["e"],
|
"entity": msg["e"],
|
||||||
}
|
}
|
||||||
|
|
||||||
await ws.send_json(msg)
|
try:
|
||||||
|
await ws.send_json(msg)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
|
||||||
|
ge_counts += 1
|
||||||
|
|
||||||
async def load_triples(running, queue, url):
|
async def load_triples(running, queue, url):
|
||||||
|
|
||||||
|
global t_counts
|
||||||
|
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
|
|
||||||
async with session.ws_connect(f"{url}load/triples") as ws:
|
async with session.ws_connect(f"{url}load/triples") as ws:
|
||||||
|
|
@ -57,6 +74,11 @@ async def load_triples(running, queue, url):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
msg = await asyncio.wait_for(queue.get(), 1)
|
msg = await asyncio.wait_for(queue.get(), 1)
|
||||||
|
|
||||||
|
# End of load
|
||||||
|
if msg is None:
|
||||||
|
break
|
||||||
|
|
||||||
except:
|
except:
|
||||||
# Hopefully it's TimeoutError. Annoying to match since
|
# Hopefully it's TimeoutError. Annoying to match since
|
||||||
# it changed in 3.11.
|
# it changed in 3.11.
|
||||||
|
|
@ -72,10 +94,12 @@ async def load_triples(running, queue, url):
|
||||||
"triples": msg["t"],
|
"triples": msg["t"],
|
||||||
}
|
}
|
||||||
|
|
||||||
await ws.send_json(msg)
|
try:
|
||||||
|
await ws.send_json(msg)
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
|
||||||
ge_counts = 0
|
t_counts += 1
|
||||||
t_counts = 0
|
|
||||||
|
|
||||||
async def stats(running):
|
async def stats(running):
|
||||||
|
|
||||||
|
|
@ -83,16 +107,15 @@ async def stats(running):
|
||||||
global ge_counts
|
global ge_counts
|
||||||
|
|
||||||
while running.get():
|
while running.get():
|
||||||
|
|
||||||
await asyncio.sleep(2)
|
await asyncio.sleep(2)
|
||||||
|
|
||||||
print(
|
print(
|
||||||
f"Graph embeddings: {ge_counts:10d} Triples: {t_counts:10d}"
|
f"Graph embeddings: {ge_counts:10d} Triples: {t_counts:10d}"
|
||||||
)
|
)
|
||||||
|
|
||||||
async def loader(running, ge_queue, t_queue, path, format, user, collection):
|
async def loader(running, ge_queue, t_queue, path, format, user, collection):
|
||||||
|
|
||||||
global t_counts
|
|
||||||
global ge_counts
|
|
||||||
|
|
||||||
if format == "json":
|
if format == "json":
|
||||||
|
|
||||||
raise RuntimeError("Not implemented")
|
raise RuntimeError("Not implemented")
|
||||||
|
|
@ -118,16 +141,18 @@ async def loader(running, ge_queue, t_queue, path, format, user, collection):
|
||||||
|
|
||||||
if unpacked[0] == "t":
|
if unpacked[0] == "t":
|
||||||
qtype = t_queue
|
qtype = t_queue
|
||||||
t_counts += 1
|
|
||||||
else:
|
else:
|
||||||
if unpacked[0] == "ge":
|
if unpacked[0] == "ge":
|
||||||
qtype = ge_queue
|
qtype = ge_queue
|
||||||
ge_counts += 1
|
|
||||||
|
|
||||||
while running.get():
|
while running.get():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await asyncio.wait_for(qtype.put(unpacked[1]), 0.5)
|
await asyncio.wait_for(qtype.put(unpacked[1]), 0.5)
|
||||||
|
|
||||||
|
# Successful put message, move on
|
||||||
|
break
|
||||||
|
|
||||||
except:
|
except:
|
||||||
# Hopefully it's TimeoutError. Annoying to match since
|
# Hopefully it's TimeoutError. Annoying to match since
|
||||||
# it changed in 3.11.
|
# it changed in 3.11.
|
||||||
|
|
@ -135,14 +160,40 @@ async def loader(running, ge_queue, t_queue, path, format, user, collection):
|
||||||
|
|
||||||
if not running.get(): break
|
if not running.get(): break
|
||||||
|
|
||||||
running.stop()
|
# Put 'None' on end of queue to finish
|
||||||
|
while running.get():
|
||||||
|
|
||||||
|
try:
|
||||||
|
await asyncio.wait_for(t_queue.put(None), 1)
|
||||||
|
|
||||||
|
# Successful put message, move on
|
||||||
|
break
|
||||||
|
|
||||||
|
except:
|
||||||
|
# Hopefully it's TimeoutError. Annoying to match since
|
||||||
|
# it changed in 3.11.
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Put 'None' on end of queue to finish
|
||||||
|
while running.get():
|
||||||
|
|
||||||
|
try:
|
||||||
|
await asyncio.wait_for(ge_queue.put(None), 1)
|
||||||
|
|
||||||
|
# Successful put message, move on
|
||||||
|
break
|
||||||
|
|
||||||
|
except:
|
||||||
|
# Hopefully it's TimeoutError. Annoying to match since
|
||||||
|
# it changed in 3.11.
|
||||||
|
continue
|
||||||
|
|
||||||
async def run(running, **args):
|
async def run(running, **args):
|
||||||
|
|
||||||
# Maxsize on queues reduces back-pressure so tg-load-kg-core doesn't
|
# Maxsize on queues reduces back-pressure so tg-load-kg-core doesn't
|
||||||
# grow to eat all memory
|
# grow to eat all memory
|
||||||
ge_q = asyncio.Queue(maxsize=500)
|
ge_q = asyncio.Queue(maxsize=10)
|
||||||
t_q = asyncio.Queue(maxsize=500)
|
t_q = asyncio.Queue(maxsize=10)
|
||||||
|
|
||||||
load_task = asyncio.create_task(
|
load_task = asyncio.create_task(
|
||||||
loader(
|
loader(
|
||||||
|
|
@ -170,9 +221,12 @@ async def run(running, **args):
|
||||||
|
|
||||||
stats_task = asyncio.create_task(stats(running))
|
stats_task = asyncio.create_task(stats(running))
|
||||||
|
|
||||||
await load_task
|
|
||||||
await triples_task
|
await triples_task
|
||||||
await ge_task
|
await ge_task
|
||||||
|
|
||||||
|
running.stop()
|
||||||
|
|
||||||
|
await load_task
|
||||||
await stats_task
|
await stats_task
|
||||||
|
|
||||||
async def main(running):
|
async def main(running):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue