4

I am using Python multiprocessing shared memory.

I see the following error when I try to close a SharedMemory object: BufferError: memoryview has 1 exported buffer

Can someone please let me know what this error means?

Here is how to reproduce: I am using Pyarrow

import pyarrow as pa
import pandas as pd
import numpy as np
from multiprocessing import shared_memory

a = pd.DataFrame(np.random.normal(size=(1000,1000)))
batch = pa.RecordBatch.from_pandas(a)

mock_sink = pa.MockOutputStream()
with pa.RecordBatchStreamWriter(mock_sink, batch.schema) as stream_writer:
    stream_writer.write_batch(batch)

data_size = mock_sink.size()
print(data_size)
shm_a = shared_memory.SharedMemory(create=True, size=data_size)

buf = pa.py_buffer(shm_a.buf)
stream = pa.FixedSizeBufferWriter(buf)
with pa.RecordBatchStreamWriter(stream, batch.schema) as stream_writer:
    stream_writer.write_batch(batch)
print(shm_a.name)
shm_a.close()
4
  • It's very hard to say anything without any code / minimal working example - please provide some more details. Commented Apr 1, 2022 at 5:52
  • just did. I have a dependency on pyarrow==6.0.1 Commented Apr 1, 2022 at 6:16
  • I tried to run this, but data_size is undefined - please fix this. Commented Apr 1, 2022 at 12:44
  • I just fixed it. I have no idea why that line failed to copy Commented Apr 1, 2022 at 15:16

2 Answers 2

2

I have encountered the same problem. After some digging, I found some clue:

enter image description here

From the code above, you can see that once we create a pa.py_buffer object from share memory's buf, shm.buf can't be released. After we delete that py_buffer object, it can be released successfully without throwing the BufferError exception.

So the solution I came up to solve my problem is as follows: enter image description here

This may not be a perfect solution. If anybody comes up with better ones, please post it.

Also note that the BufferError still seem to arise when we read the shared memory from multiple processes. I am still working on it.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't believe it solved my problem...
sorry, I made a mistake, just edited the answer. The solution is temporary, if you found better ones, please post it, thanks.
0

That resolved my problem but now, that I'm trying to put dataframes of pyarrow types into a shared memory I'm getting this problem again. If you learn something more about this please visit my question here: BufferError: memoryview has 1 exported buffer trying to close a shared memory where I put a dataframe of pyarrow data type values

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.