New submission from Antoine Pitrou <[EMAIL PROTECTED]>:

As discovered in #3139, io.BufferedWriter mutates the size of its
internal bytearray object. Consequently, invocations of write() from
multiple threads can produce exceptions when one thread gets a buffer to
the bytearray while the other thread tries to resize it. This especially
affects calling print() from multiple threads.

A solution is to use a fixed-size (preallocated) bytearray object.
Another solution is to get rid of the bytearray approach and replace it
with a growing list of immutable bytes objects.

Here is the test script provided by Amaury:

import sys, io, threading
stdout2 = io.open(sys.stdout.fileno(), mode="w")
def f(i):
    for i in range(10):
        stdout2.write(unicode((x, i)) + '\n')
for x in range(10):
    t = threading.Thread(target=f, args=(x,))
    t.start()

(with py3k, replace "stdout2.write" with a simple "print")

----------
components: Library (Lib)
messages: 70494
nosy: amaury.forgeotdarc, pitrou
priority: high
severity: normal
status: open
title: BufferedWriter not thread-safe
type: behavior
versions: Python 2.6, Python 3.0

_______________________________________
Python tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue3476>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to