Skip to content

Commit d11afa9

Browse files
authored
Merge pull request #189 from openzim/threadunsafe
Add thread safety note on README
2 parents 3b8645b + 7fe5fea commit d11afa9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,25 @@ with Creator("test.zim").config_indexing(True, "eng") as creator:
127127
creator.add_metadata(name.title(), value)
128128
```
129129

130+
#### Thread safety
131+
132+
> 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)
133+
134+
`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)
135+
136+
```py
137+
lock = threading.Lock()
138+
creator = Creator("test.zim")
139+
140+
# Thread #1
141+
with lock:
142+
creator.add_item(item1)
143+
144+
# Thread #2
145+
with lock:
146+
creator.add_item(item2)
147+
```
148+
130149
## Building
131150

132151
`libzim` package building offers different behaviors via environment variables

0 commit comments

Comments
 (0)