Python threading with strange output errors
<p>I am new to threading in general and have been playing around with different ideas to get my feet under me. However, I came across something I am not sure how to explain. Here is the code:</p>

<pre><code>import threading, datetime
class ThreadClass(threading.Thread):
    def run(self):
        for _ in range(3):
            now = datetime.datetime.now()
            print "%s: %s" %(self.getName(), now)

for i in range(2):
    t = ThreadClass()
    t.start()
</code></pre>

<p>The program does exactly what I wanted it to do, however, the output has some random errors in it.</p>

<pre><code>Thread-1: 2016-10-12 17:34:23.012462
Thread-1: 2016-10-12 17:34:23.012802
 Thread-2: 2016-10-12 17:34:23.013025
Thread-2: 2016-10-12 17:34:23.013108

Thread-2: 2016-10-12 17:34:23.013225
Thread-1: 2016-10-12 17:34:23.013252
</code></pre>

<p>The errors are relatively consistent (i.e. the same space and new line show up in the output, just in different places). I suspect it has something to do with the threads trying to write to the output stream at about the same time, but in this particular run both errors occur when Thread-2 is running.</p>

<p>Any assistance in describe why this issue is occuring is greatly appreciated!</p>

<p>I am running this code with Python 2.7 on Ubuntu 14.04 (dual-core CPU), if that would make any difference.</p>

<p>Part of your question is <strong>why</strong> is this occurring?  And its a good question as you'll notice in your output there is a completely spurious space that <em>neither of the threads actually tried to print</em></p>

<pre><code>Thread-1: 2016-10-12 17:34:23.012802
 Thread-2: 2016-10-12 17:34:23.013025
^
</code></pre>

<p>How did that get there?  None of your threads tried to print it!  Its due to the way the <code>print</code> statement in Python 2.x implements <em>soft spacing</em>.</p>

<p>For example ... when you execute <code>print 1,</code> you receive on <code>stdout</code></p>

<pre><code>1              # there is no space after that 1 yet!
</code></pre>

<p>Then a subsequent <code>print 2</code> will cause a space to be inserted to give a final output of:</p>

<pre><code>1 2
</code></pre>

<p>What is happening is that a <code>print &lt;something&gt;,</code> statement gets compiled to the bytecode operation <code>PRINT_ITEM</code>.  The implementation of <code>PRINT_ITEM</code>:</p>

<ul>
<li>checks the output stream to see if the last <code>PRINT_ITEM</code> to this stream marked it as needing a soft space printed;</li>
<li>if it does, print a " ";</li>
<li>then print our item and remark the stream as needing another soft space.</li>
</ul>

<p>And there is another bytecode operation <code>PRINT_NEWLINE</code> which prints a new line and clears any soft space marker on the stream.</p>

<p>Now in your code, each thread will do the following:</p>

<pre><code>print "%s: %s" %(self.getName(), now)
</code></pre>

<p>This gets compiled to:</p>

<pre><code>PRINT_ITEM          # print the top of the stack
PRINT_NEWLINE       # print a new line
</code></pre>

<p>Thus, thread interaction can mean that:</p>

<ul>
<li><code>Thread-1</code> can perform <code>PRINT_ITEM</code> and mark the stream as needing a softspace for the next <code>PRINT_ITEM</code>;</li>
<li><code>Thread-2</code> can begin <code>PRINT_ITEM</code>, and see the stream needs a soft space;</li>
<li><code>Thread-1</code> now prints a new line;</li>
<li><code>Thread-2</code> now prints that space and its item;</li>
</ul>

<p>Note that this error would not occur if:</p>

<ul>
<li>you used <code>sys.stdout.write</code> to perform your output; or</li>
<li>you were using Python 3.x which has a different io structure</li>
</ul>

python multithreading