If print s
is replaced by print >>sys.stderr, s
then the effect vanishes.
import random, sys, time
import threading
lock = threading.Lock()
def echo(s):
time.sleep(1e-3*random.random()) # instead of threading.Timer()
with lock:
print s
for c in 'abc':
threading.Thread(target=echo, args=(c,)).start()
Example
# Run until empty line is found:
$ while ! python example.py 2>&1|tee out|grep '^$';do echo -n .;done;cat out
Output
....................
b
c
a
The output should not contain empty lines, but it does. I understand that print
is not thread-safe, but I would've thought the lock should help.
The question is why this happens?
My machine:
$ python -mplatform
Linux-2.6.38-11-generic-x86_64-with-Ubuntu-11.04-natty
Extra lines are printed on py26, py27, pypy.
py24, py25, py31, py32 behave as expected (no empty lines).
Variations
sys.stdout.flush()
after theprint
doesn't solve the problem:with lock: print(s) sys.stdout.flush()
even more strange that ordinary
sys.stdout.write()
doesn't produce empty lines with lock:with lock: sys.stdout.write(s) sys.stdout.write('\n') #NOTE: no .flush()
print
function works as expected (no empty lines).
To reproduce download files and run:
$ tox
tox.ini
. Could you run the test? – Healthsoftspace
. There's probably good reason print() was revamped in Python 3. – Shurlocke