kuzu icon indicating copy to clipboard operation
kuzu copied to clipboard

Resource contention on Windows using the Python API

Open ZmeiGorynych opened this issue 9 months ago • 10 comments

When I try to insert nodes and relations in a loop, like below, sooner or later I get the following exception: (<class 'RuntimeError'>, RuntimeError('remove: The process cannot access the file because it is being used by another process.: "C:\\Users\\Egor\\Dropbox\\Code\\motleycrew\\examples\\bug_db\\nodes.statistics_and_deleted.ids.wal"'), <traceback object at 0x000001E21B6AB280>) This only happens on Windows (at least not on a Mac), but on Windows it always does :(. Sleeping between inserts makes the problem less likely, but it still appears sometimes.

Running kuzu==0.3.2 installed with pip, on Python 3.11 on Window.s

import shutil
from pathlib import Path
from time import sleep
import os
import shutil

import kuzu

WORKING_DIR = Path(__file__).parent
DB_PATH = WORKING_DIR / "bug_db"
node_table_name = "entity"
rel_table_name = "links"

if os.path.isdir(DB_PATH):
    shutil.rmtree(DB_PATH)


database = kuzu.Database(DB_PATH)
connection = kuzu.Connection(database)
node_tables = connection._get_node_table_names()

if node_table_name not in node_tables:
    connection.execute("CREATE NODE TABLE %s (ID STRING, PRIMARY KEY(ID))" % node_table_name)
rel_tables = connection._get_rel_table_names()
rel_tables = [rel_table["name"] for rel_table in rel_tables]
if rel_table_name not in rel_tables:
    connection.execute(
        "CREATE REL TABLE {} (FROM {} TO {}, predicate STRING)".format(
            rel_table_name, node_table_name, node_table_name
        )
    )


def create_relation(subj: str, obj: str, rel: str) -> None:
    connection.execute(
        (
            "MATCH (n1:{}), (n2:{}) WHERE n1.ID = $subj AND n2.ID = $obj "
            "CREATE (n1)-[r:{} {{predicate: $pred}}]->(n2)"
        ).format(node_table_name, node_table_name, rel_table_name),
        {"subj": subj, "obj": obj, "pred": rel},
    )


def create_entity(entity: str) -> None:
    connection.execute(
        "CREATE (n:%s {ID: $entity})" % node_table_name,
        {"entity": entity},
    )


create_entity("entity0")

sleep_time = 0
for i in range(10):
    print(i + 1)
    create_entity(f"entity{i+1}")
    sleep(sleep_time)
    create_relation("entity1", f"entity{i+1}", "rel1")
    sleep(sleep_time)

ZmeiGorynych avatar Apr 28 '24 13:04 ZmeiGorynych