[issue3139] bytearrays are not thread safe
report at bugs.python.org
Thu Jul 31 02:16:35 CEST 2008
Antoine Pitrou <pitrou at free.fr> added the comment:
Le jeudi 31 juillet 2008 à 00:00 +0000, Amaury Forgeot d'Arc a écrit :
> Amaury Forgeot d'Arc <amauryfa at gmail.com> added the comment:
> > If it was rewritten to use a fixed-size bytearray
> does such an object exist today?
Manually, yes :)
Just preallocate your bytearray, e.g.:
b = bytearray(b" " * 4096)
and then be careful to only do operations (e.g. slice assignments) which
keep the size intact.
In a BufferedWriter implementation, you would have to keep track of the
currently used chunk in the bytearray (offset and size).
Anyway, I'd question the efficiency of the bytearray approach; when
removing the quadratic behaviour in BufferedReader I discovered that
using a bytearray was slower than keeping a list of bytes instances and
joining them when needed (not to mention that the latter is inherently
thread-safe :-)). The reason is that the underlying raw stream expects
and returns bytes, and the public buffered API also does, so using a
bytearray internally means lots of additional memory copies.
(a related problem is that readinto() does not let you specify an offset
inside the given buffer object, it always starts writing at the
beginning of the buffer. Perhaps memoryview() will support creating
subbuffers, I don't know...)
Python tracker <report at bugs.python.org>
More information about the Python-bugs-list