y-crdt / ypy

Python bindings to y-crdt
https://ypy.readthedocs.io/en/latest/
Other
184 stars 22 forks source link

ERROR message "already borrowed" when trying to update a map in a callback #154

Open scenaristeur opened 7 months ago

scenaristeur commented 7 months ago

seems to be similar to #69 I have three maps like a kanban : todos, doing, done and i want a worker to be able to "move" a task from "todos" to "doing" but i've got an "already borrowed" error and i don't know how to deal with it

here is my code

import asyncio
import y_py as Y
from websockets import connect
from ypy_websocket import WebsocketProvider

async def client():
    ydoc = Y.YDoc()
    async with (
        connect("ws://localhost:1234/my-roomname") as websocket,
        WebsocketProvider(ydoc, websocket),
    ):
        def callback(e):
            print("\n-----------")
            print("TODOS",todos)
            print("DOING",doing)
            print("DONE",done)
            #print("\nKEYS",todos.keys())
            #print("\nEVENTS",e.keys)
            for id , task in todos.items():
                print("------1 todo",id, "->", task)
                try:
                    ydoc.transact(lambda txn: todos.pop(txn, id))
                    ydoc.transact(lambda txn: doing.set(txn, id, task))
                except Exception as e:
                    print("ERROR", e)

        todos = ydoc.get_map("todos")
        doing = ydoc.get_map("doing")
        done = ydoc.get_map("done")

        todos.observe(callback)
        doing.observe(callback)
        done.observe(callback)

        await asyncio.Future()  # run forever

asyncio.run(client())

and the log :

-----------
TODOS {'cb3efbd1-a2cd-48e0-960f-9f1f1247ab70': {'created': 1711295273830.0, 'text': 'Bonjour', 'asker': 'c4fbbffd-c97b-4e3c-b769-9a7fc5420fb9', 'id': 'cb3efbd1-a2cd-48e0-960f-9f1f1247ab70'}, '934e1974-69ac-431e-b234-c290b006ca55': {'asker': 'c4fbbffd-c97b-4e3c-b769-9a7fc5420fb9', 'id': '934e1974-69ac-431e-b234-c290b006ca55', 'text': 'Bonjour', 'created': 1711295173598.0}}
DOING {}
DONE {}
------1 todo 934e1974-69ac-431e-b234-c290b006ca55 -> {'asker': 'c4fbbffd-c97b-4e3c-b769-9a7fc5420fb9', 'created': 1711295173598.0, 'text': 'Bonjour', 'id': '934e1974-69ac-431e-b234-c290b006ca55'}
ERROR Already borrowed
------1 todo cb3efbd1-a2cd-48e0-960f-9f1f1247ab70 -> {'created': 1711295273830.0, 'text': 'Bonjour', 'asker': 'c4fbbffd-c97b-4e3c-b769-9a7fc5420fb9', 'id': 'cb3efbd1-a2cd-48e0-960f-9f1f1247ab70'}
ERROR Already borrowed

-----------
TODOS {'934e1974-69ac-431e-b234-c290b006ca55': {'asker': 'c4fbbffd-c97b-4e3c-b769-9a7fc5420fb9', 'text': 'Bonjour', 'created': 1711295173598.0, 'id': '934e1974-69ac-431e-b234-c290b006ca55'}, 'cb3efbd1-a2cd-48e0-960f-9f1f1247ab70': {'id': 'cb3efbd1-a2cd-48e0-960f-9f1f1247ab70', 'text': 'Bonjour', 'asker': 'c4fbbffd-c97b-4e3c-b769-9a7fc5420fb9', 'created': 1711295273830.0}}
DOING {}
DONE {}
------1 todo 934e1974-69ac-431e-b234-c290b006ca55 -> {'asker': 'c4fbbffd-c97b-4e3c-b769-9a7fc5420fb9', 'created': 1711295173598.0, 'id': '934e1974-69ac-431e-b234-c290b006ca55', 'text': 'Bonjour'}
ERROR Already borrowed
------1 todo cb3efbd1-a2cd-48e0-960f-9f1f1247ab70 -> {'id': 'cb3efbd1-a2cd-48e0-960f-9f1f1247ab70', 'created': 1711295273830.0, 'asker': 'c4fbbffd-c97b-4e3c-b769-9a7fc5420fb9', 'text': 'Bonjour'}
ERROR Already borrowed
davidbrochart commented 7 months ago

Ypy is unmaintained, you may want to look at pycrdt.