slick-queue-py 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- slick_queue_py-1.0.0/.github/FUNDING.yml +15 -0
- slick_queue_py-1.0.0/.github/workflows/ci.yml +32 -0
- slick_queue_py-1.0.0/.gitignore +243 -0
- slick_queue_py-1.0.0/API_DIFFERENCES.md +249 -0
- slick_queue_py-1.0.0/BUILDING.md +242 -0
- slick_queue_py-1.0.0/CMakeLists.txt +13 -0
- slick_queue_py-1.0.0/LICENSE +21 -0
- slick_queue_py-1.0.0/MANIFEST.in +33 -0
- slick_queue_py-1.0.0/PKG-INFO +805 -0
- slick_queue_py-1.0.0/README.md +769 -0
- slick_queue_py-1.0.0/SOLUTION.md +51 -0
- slick_queue_py-1.0.0/atomic_ops.py +609 -0
- slick_queue_py-1.0.0/atomic_ops_ext.cpp +137 -0
- slick_queue_py-1.0.0/pyproject.toml +44 -0
- slick_queue_py-1.0.0/requirements.txt +2 -0
- slick_queue_py-1.0.0/setup.cfg +4 -0
- slick_queue_py-1.0.0/setup.py +68 -0
- slick_queue_py-1.0.0/slick_queue_py.egg-info/SOURCES.txt +28 -0
- slick_queue_py-1.0.0/slick_queue_py.py +520 -0
- slick_queue_py-1.0.0/tests/cleanup_shm.py +53 -0
- slick_queue_py-1.0.0/tests/cpp_consumer.cpp +131 -0
- slick_queue_py-1.0.0/tests/cpp_multi_producer.cpp +122 -0
- slick_queue_py-1.0.0/tests/cpp_producer.cpp +94 -0
- slick_queue_py-1.0.0/tests/cpp_work_stealing_consumer.cpp +141 -0
- slick_queue_py-1.0.0/tests/run_test.py +67 -0
- slick_queue_py-1.0.0/tests/test_atomic_cursor.py +715 -0
- slick_queue_py-1.0.0/tests/test_atomic_ops.py +367 -0
- slick_queue_py-1.0.0/tests/test_interop.py +704 -0
- slick_queue_py-1.0.0/tests/test_local_mode.py +42 -0
- slick_queue_py-1.0.0/tests/test_multi_producer.py +733 -0
- slick_queue_py-1.0.0/tests/test_queue.py +43 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# These are supported funding model platforms
|
|
2
|
+
|
|
3
|
+
github: SlickQuant
|
|
4
|
+
patreon: # Replace with a single Patreon username
|
|
5
|
+
open_collective: # Replace with a single Open Collective username
|
|
6
|
+
ko_fi: # Replace with a single Ko-fi username
|
|
7
|
+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
|
|
8
|
+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
|
|
9
|
+
liberapay: # Replace with a single Liberapay username
|
|
10
|
+
issuehunt: # Replace with a single IssueHunt username
|
|
11
|
+
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
|
|
12
|
+
polar: # Replace with a single Polar username
|
|
13
|
+
buy_me_a_coffee: # Replace with a single Buy Me a Coffee username
|
|
14
|
+
thanks_dev: # Replace with a single thanks.dev username
|
|
15
|
+
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ main ]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [ main ]
|
|
8
|
+
|
|
9
|
+
permissions:
|
|
10
|
+
contents: read
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build-and-test:
|
|
14
|
+
strategy:
|
|
15
|
+
matrix:
|
|
16
|
+
os: [ubuntu-latest, windows-latest, macos-latest]
|
|
17
|
+
build_type: [Release, Debug]
|
|
18
|
+
|
|
19
|
+
runs-on: ${{ matrix.os }}
|
|
20
|
+
|
|
21
|
+
steps:
|
|
22
|
+
- uses: actions/checkout@v4
|
|
23
|
+
|
|
24
|
+
- name: Configure CMake
|
|
25
|
+
run: cmake -B build -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
|
|
26
|
+
|
|
27
|
+
- name: Build
|
|
28
|
+
run: cmake --build build --config ${{ matrix.build_type }}
|
|
29
|
+
|
|
30
|
+
- name: Run Tests
|
|
31
|
+
working-directory: build
|
|
32
|
+
run: ctest -C ${{ matrix.build_type }} --output-on-failure
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
|
208
|
+
|
|
209
|
+
dist/
|
|
210
|
+
build/
|
|
211
|
+
*.DS_Store
|
|
212
|
+
.vscode/
|
|
213
|
+
.idea/
|
|
214
|
+
|
|
215
|
+
# CMake build artifacts
|
|
216
|
+
CMakeCache.txt
|
|
217
|
+
CMakeFiles/
|
|
218
|
+
cmake_install.cmake
|
|
219
|
+
Makefile
|
|
220
|
+
*.cmake
|
|
221
|
+
!CMakeLists.txt
|
|
222
|
+
|
|
223
|
+
# C++ build artifacts
|
|
224
|
+
*.o
|
|
225
|
+
*.obj
|
|
226
|
+
*.exe
|
|
227
|
+
*.out
|
|
228
|
+
*.app
|
|
229
|
+
cpp_producer
|
|
230
|
+
cpp_consumer
|
|
231
|
+
cpp_multi_producer
|
|
232
|
+
|
|
233
|
+
# Test artifacts
|
|
234
|
+
tests/*.txt
|
|
235
|
+
!tests/CMakeLists.txt
|
|
236
|
+
!tests/*.py
|
|
237
|
+
!tests/*.cpp
|
|
238
|
+
|
|
239
|
+
# Shared memory cleanup
|
|
240
|
+
*.shm
|
|
241
|
+
|
|
242
|
+
# File indicating readiness
|
|
243
|
+
ready
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
# C++ / Python API Differences
|
|
2
|
+
|
|
3
|
+
This document explains the intentional API differences between the C++ `slick::SlickQueue<T>` and Python `SlickQueue` implementations.
|
|
4
|
+
|
|
5
|
+
## Core Compatibility
|
|
6
|
+
|
|
7
|
+
Both implementations:
|
|
8
|
+
- ✅ Use identical memory layouts
|
|
9
|
+
- ✅ Use identical atomic operations
|
|
10
|
+
- ✅ Are fully interoperable (C++ ↔ Python communication works)
|
|
11
|
+
- ✅ Support lock-free multi-producer multi-consumer semantics
|
|
12
|
+
|
|
13
|
+
## API Differences
|
|
14
|
+
|
|
15
|
+
### 1. Constructor / Opening Queues
|
|
16
|
+
|
|
17
|
+
**C++:**
|
|
18
|
+
```cpp
|
|
19
|
+
// Create new queue in shared memory
|
|
20
|
+
slick::SlickQueue<uint8_t> q(queue_size, queue_name);
|
|
21
|
+
|
|
22
|
+
// Open existing queue
|
|
23
|
+
slick::SlickQueue<uint8_t> q(queue_name);
|
|
24
|
+
|
|
25
|
+
// Local memory mode
|
|
26
|
+
slick::SlickQueue<uint8_t> q(queue_size); // No shared memory
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Python:**
|
|
30
|
+
```python
|
|
31
|
+
# Create new queue in shared memory
|
|
32
|
+
q = SlickQueue(name=queue_name, size=queue_size, element_size=element_size, create=True)
|
|
33
|
+
|
|
34
|
+
# Open existing queue
|
|
35
|
+
q = SlickQueue(name=queue_name, element_size=element_size)
|
|
36
|
+
|
|
37
|
+
# Local memory mode
|
|
38
|
+
q = SlickQueue(size=queue_size, element_size=element_size) # No shared memory
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**Rationale:** Python uses keyword arguments for clarity and supports both shared memory and local memory modes.
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
### 2. read() Method - Return Value vs. Reference Parameter
|
|
46
|
+
|
|
47
|
+
**C++:**
|
|
48
|
+
```cpp
|
|
49
|
+
uint64_t read_index = 0;
|
|
50
|
+
|
|
51
|
+
// read() returns (data, size) and updates read_index by reference
|
|
52
|
+
auto [data, size] = queue.read(read_index); // read_index modified in-place
|
|
53
|
+
|
|
54
|
+
if (data != nullptr) {
|
|
55
|
+
process(data, size);
|
|
56
|
+
// read_index has been updated automatically
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
**Python:**
|
|
61
|
+
```python
|
|
62
|
+
read_index = 0
|
|
63
|
+
|
|
64
|
+
# read() returns (data, size, new_read_index)
|
|
65
|
+
data, size, read_index = q.read(read_index) # Returns new index
|
|
66
|
+
|
|
67
|
+
if data is not None:
|
|
68
|
+
process(data, size)
|
|
69
|
+
# read_index now holds the new value
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**Rationale:**
|
|
73
|
+
- Python doesn't have true pass-by-reference for primitive types
|
|
74
|
+
- Returning the new index is the Pythonic pattern (like `list.pop()`, `str.split()`, etc.)
|
|
75
|
+
- The assignment `read_index = q.read(read_index)` is clear and explicit
|
|
76
|
+
|
|
77
|
+
**Important:** This is a **usage difference**, not a compatibility issue. The underlying memory operations are identical. When C++ and Python processes communicate:
|
|
78
|
+
- C++ `read()` and Python `read()` both read from the same atomic slots
|
|
79
|
+
- Both update their local `read_index` variable correctly
|
|
80
|
+
- The memory layout and atomic semantics are identical
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
### 3. Element Access
|
|
85
|
+
|
|
86
|
+
**C++:**
|
|
87
|
+
```cpp
|
|
88
|
+
auto idx = queue.reserve();
|
|
89
|
+
uint8_t* slot = queue[idx]; // Returns pointer
|
|
90
|
+
std::memcpy(slot, data, data_len);
|
|
91
|
+
queue.publish(idx);
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
**Python:**
|
|
95
|
+
```python
|
|
96
|
+
idx = q.reserve()
|
|
97
|
+
slot = q[idx] # Returns memoryview
|
|
98
|
+
slot[:data_len] = data
|
|
99
|
+
q.publish(idx)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
**Rationale:** Python uses `memoryview` for safe buffer access instead of raw pointers.
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
### 4. Type Safety
|
|
107
|
+
|
|
108
|
+
**C++:**
|
|
109
|
+
```cpp
|
|
110
|
+
// Template-based, compile-time type safety
|
|
111
|
+
slick::SlickQueue<int32_t> q(1024, "queue");
|
|
112
|
+
auto idx = q.reserve();
|
|
113
|
+
*q[idx] = 42; // Type-safe int32_t access
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
**Python:**
|
|
117
|
+
```python
|
|
118
|
+
# Element size specified, runtime packing/unpacking
|
|
119
|
+
q = SlickQueue(name='queue', size=1024, element_size=4)
|
|
120
|
+
idx = q.reserve()
|
|
121
|
+
import struct
|
|
122
|
+
q[idx][:4] = struct.pack("<i", 42) # Manual packing
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
**Rationale:** Python doesn't have templates. Users explicitly handle serialization with `struct` module.
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
### 5. Resource Management
|
|
130
|
+
|
|
131
|
+
**C++:**
|
|
132
|
+
```cpp
|
|
133
|
+
{
|
|
134
|
+
slick::SlickQueue<T> q(size, "name");
|
|
135
|
+
// ... use queue ...
|
|
136
|
+
} // Destructor automatically cleans up
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
**Python:**
|
|
140
|
+
```python
|
|
141
|
+
# Option 1: Manual cleanup
|
|
142
|
+
q = SlickQueue(name='name', size=size, element_size=elem_size, create=True)
|
|
143
|
+
try:
|
|
144
|
+
# ... use queue ...
|
|
145
|
+
finally:
|
|
146
|
+
q.close()
|
|
147
|
+
q.unlink() # Only call from creator process
|
|
148
|
+
|
|
149
|
+
# Option 2: Context manager (recommended)
|
|
150
|
+
with SlickQueue(name='name', size=size, element_size=elem_size, create=True) as q:
|
|
151
|
+
# ... use queue ...
|
|
152
|
+
pass # Automatic cleanup
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
**Rationale:** Python's garbage collection is non-deterministic, so explicit cleanup or context managers are needed.
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## Interoperability Examples
|
|
160
|
+
|
|
161
|
+
### Example 1: C++ Producer → Python Consumer
|
|
162
|
+
|
|
163
|
+
**C++ (producer.cpp):**
|
|
164
|
+
```cpp
|
|
165
|
+
#include <slick/queue.h>
|
|
166
|
+
#include <cstring>
|
|
167
|
+
|
|
168
|
+
int main() {
|
|
169
|
+
slick::SlickQueue<uint8_t> queue("my_queue"); // Open existing queue
|
|
170
|
+
|
|
171
|
+
for (int i = 0; i < 100; i++) {
|
|
172
|
+
auto idx = queue.reserve();
|
|
173
|
+
uint32_t value = i;
|
|
174
|
+
std::memcpy(queue[idx], &value, sizeof(value));
|
|
175
|
+
queue.publish(idx);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
**Python (consumer.py):**
|
|
181
|
+
```python
|
|
182
|
+
from slick_queue_py import SlickQueue
|
|
183
|
+
import struct
|
|
184
|
+
|
|
185
|
+
# Create queue (C++ will open it)
|
|
186
|
+
q = SlickQueue(name='my_queue', size=64, element_size=32, create=True)
|
|
187
|
+
|
|
188
|
+
read_index = 0
|
|
189
|
+
for _ in range(100):
|
|
190
|
+
data, size, read_index = q.read(read_index)
|
|
191
|
+
if data:
|
|
192
|
+
value = struct.unpack("<I", data[:4])[0]
|
|
193
|
+
print(f"Received: {value}")
|
|
194
|
+
|
|
195
|
+
q.close()
|
|
196
|
+
q.unlink()
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Example 2: Python Producer → C++ Consumer
|
|
200
|
+
|
|
201
|
+
**Python (producer.py):**
|
|
202
|
+
```python
|
|
203
|
+
from slick_queue_py import SlickQueue
|
|
204
|
+
import struct
|
|
205
|
+
|
|
206
|
+
q = SlickQueue(name='my_queue', size=64, element_size=32, create=True)
|
|
207
|
+
|
|
208
|
+
for i in range(100):
|
|
209
|
+
idx = q.reserve()
|
|
210
|
+
q[idx][:4] = struct.pack("<I", i)
|
|
211
|
+
q.publish(idx)
|
|
212
|
+
|
|
213
|
+
q.close()
|
|
214
|
+
# Don't unlink yet - C++ will use it
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
**C++ (consumer.cpp):**
|
|
218
|
+
```cpp
|
|
219
|
+
#include <slick/queue.h>
|
|
220
|
+
#include <iostream>
|
|
221
|
+
#include <cstring>
|
|
222
|
+
|
|
223
|
+
int main() {
|
|
224
|
+
slick::SlickQueue<uint8_t> queue("my_queue"); // Open existing queue
|
|
225
|
+
|
|
226
|
+
uint64_t read_index = 0;
|
|
227
|
+
for (int i = 0; i < 100; i++) {
|
|
228
|
+
auto [data, size] = queue.read(read_index); // Updates read_index
|
|
229
|
+
|
|
230
|
+
if (data) {
|
|
231
|
+
uint32_t value;
|
|
232
|
+
std::memcpy(&value, data, sizeof(value));
|
|
233
|
+
std::cout << "Received: " << value << std::endl;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## Summary
|
|
240
|
+
|
|
241
|
+
| Feature | C++ | Python | Reason for Difference |
|
|
242
|
+
|---------|-----|--------|---------------------|
|
|
243
|
+
| `read()` return | `(data, size)` | `(data, size, new_index)` | Python has no pass-by-reference |
|
|
244
|
+
| Constructor | `(size, name)` or `(name)` | Named parameters with `create` flag | Python idioms |
|
|
245
|
+
| Element access | Pointer | `memoryview` | Python memory safety |
|
|
246
|
+
| Type safety | Templates | `struct` module | Language difference |
|
|
247
|
+
| Cleanup | RAII (destructor) | Explicit or context manager | Python GC is non-deterministic |
|
|
248
|
+
|
|
249
|
+
**Bottom Line:** The Python API is designed to be Pythonic while maintaining **100% binary compatibility** with C++. All atomic operations and memory layouts are identical, enabling seamless C++/Python interoperability.
|