From 7fe5fea62c0a09651539c16bf82eaa999a4d2a55 Mon Sep 17 00:00:00 2001 From: renaud gaudin Date: Mon, 12 Feb 2024 17:11:53 +0000 Subject: [PATCH] Add thread safety note on README --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 4211ce4b..4eefcbfc 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,25 @@ with Creator("test.zim").config_indexing(True, "eng") as creator: creator.add_metadata(name.title(), value) ``` +#### Thread safety + +> The reading part of the libzim is most of the time thread safe. Searching and creating part are not. [libzim documentation](https://libzim.readthedocs.io/en/latest/usage.html#introduction) + +`python-libzim` disables the [GIL](https://wiki.python.org/moin/GlobalInterpreterLock) on most of C++ libzim calls. You **must prevent concurrent access** yourself. This is easily done by wrapping all creator calls with a [`threading.Lock()`](https://docs.python.org/3/library/threading.html#lock-objects) + +```py +lock = threading.Lock() +creator = Creator("test.zim") + +# Thread #1 +with lock: + creator.add_item(item1) + +# Thread #2 +with lock: + creator.add_item(item2) +``` + ## Building `libzim` package building offers different behaviors via environment variables