Multiprocess mode: using direct assignment for writing into mmap #315
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@brian-brazil
Using
struct.pack_into
can create atomicity problems when writing on CPython, setting the bytes to 0 before writing the value.When having a lot of writes, some reads would return 0 instead of the real value. This line in CPython is suspected: https://github.com/python/cpython/blob/2.7/Modules/_struct.c#L1563
You can reproduce the issue by having two scripts running in two different processes but using the same file, one writing an always incrementing counter and the other reading it. Sometimes, the reader script will read 0 instead of the real value. We encountered this in production by running a Django web service behind gunicorn.
Example writer script:
Example reader script:
If you execute both of these at the same time, you will see the number of 0 reads increasing. This PR fixes the issue.