FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Update README with thread safety section by lysnikolaou · Pull Request #657 · msgpack/msgpack-python · GitHub

Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .md  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
35 changes: 35 additions & 0 deletions README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,41 @@ However, a tuple is lighter than a list.
You can use `use_list=False` while unpacking when performance is important.


### Thread safety (CPython 3.14+ free-threaded build)

msgpack is designed to be thread-safe when used with CPython 3.14's
free-threaded build (PEP 703).

#### Thread-Safety Guarantees

* **Individual `Packer` and `Unpacker` instances are thread-safe**:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

thread-safe is a very strong guarantee. Packer and Unpacker are designed to not crash when used from multiple threads, but the correctness of the output depends on the types and timing of the methods being called simultaneously.

You can safely call methods on the same `Packer` or `Unpacker` instance
from multiple threads concurrently. All public methods are protected by
critical sections that ensure atomic access to internal state.

* **Module-level functions are thread-safe**: Functions like `packb()` and
`unpackb()` create their own instances internally and are safe to call
from multiple threads.

#### Performance Considerations

While sharing a single `Packer` or `Unpacker` instance across threads is safe,
it may serialize operations due to critical section locking. For optimal
performance in multi-threaded applications, create separate instances per thread:

```python

Copilot AI Nov 29, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The code block uses python which is inconsistent with the existing README convention. Other non-interactive code examples in this README use py (see lines 60, 81). For consistency, this should be changed to ```py.

Suggested change
```python
```py

Copilot uses AI. Check for mistakes.
import msgpack
from concurrent.futures import ThreadPoolExecutor

def worker(data):
# Each thread creates its own packer
packer = msgpack.Packer()
return packer.pack(data)

with ThreadPoolExecutor(max_workers=4) as executor:
results = executor.map(worker, <data>)

Copilot AI Nov 29, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The code example contains a syntax error with the placeholder <data>. This should be replaced with a concrete example like [1, 2, 3, 4] or data_items to make the example functional and executable.

Suggested change
results = executor.map(worker, <data>)
results = executor.map(worker, [1, 2, 3, 4])

Copilot uses AI. Check for mistakes.

Copilot AI Nov 29, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The new thread safety documentation and recommended usage pattern lacks test coverage. Consider adding a test case that validates thread-safe usage of Packer and Unpacker instances across multiple threads, similar to the example provided in the documentation.

Copilot uses AI. Check for mistakes.
```

## Major breaking changes in the history

### msgpack 0.5
Expand Down

Back | FazBrowse Home | New Git URL