4

I've worked with Python for a while, but I've never really done any concurrency in it before today. I stumbled upon this blog post and decided to make a similar (but simpler) example:

import os
import threading
import Queue

class Worker(threading.Thread):
    def __init__(self, queue, num):
        threading.Thread.__init__(self)
        self.queue = queue
        self.num = num

    def run(self):
        while True:
            text = self.queue.get()
            #print "{} :: {}".format(self.num, text)
            print "%s :: %s" % (self.num, text)
            self.queue.task_done()

nonsense = ["BLUBTOR", "more nonsense", "cookies taste good", "what is?!"]
queue = Queue.Queue()

for i in xrange(4):
    # Give the worker the queue and also its "number"
    t = Worker(queue, i)
    t.setDaemon(True)
    t.start()

for gibberish in nonsense:
    queue.put(gibberish)

queue.join()

It seems to work fine, but there seems to be some problem with the prints which I cannot figure out. A couple of test runs:

chris@DPC3:~/code/pythonthreading$ python owntest.py 
0 :: BLUBTOR
 1 :: more nonsense
3 :: cookies taste good
 2 :: what is?!
chris@DPC3:~/code/pythonthreading$ python owntest.py 
0 :: BLUBTOR
2 :: more nonsense
3 :: cookies taste good0 :: what is?!

chris@DPC3:~/code/pythonthreading$ python owntest.py 
2 :: BLUBTOR
 3 :: more nonsense1 :: cookies taste good

 2 :: what is?!
chris@DPC3:~/code/pythonthreading$

Why is the output formatted this oddly?

4

2 回答 2

6

print不是原子的。

以下行:

        print "%s :: %s" % (self.num, text)

被翻译成以下字节码:

         24 LOAD_CONST               1 ('%s :: %s')
         27 LOAD_FAST                0 (self)
         30 LOAD_ATTR                3 (num)
         33 LOAD_FAST                1 (text)
         36 BUILD_TUPLE              2
         39 BINARY_MODULO       
         40 PRINT_ITEM          
         41 PRINT_NEWLINE       

如您所见,那里有两个打印字节码(PRINT_ITEMPRINT_NEWLINE)。如果线程在两者之间被抢占,你会看到你所看到的。

我同意其他人sys.stdout.write()对此用例更安全的选择,因为:

  1. 它迫使您在编写整个字符串之前对其进行格式化(print您可能会不小心使用print a, b, c,并最终得到三个单独的写入而不是一个);
  2. 它回避了软空间和自动换行的问题,这两者都可以与print程序其他部分的语句交互。
于 2013-01-06T10:51:41.943 回答
6

打印不是线程安全的。

当一些字符被一个线程复制到stdout流中时,另一个线程被调度,它也在打印,它可以将字符复制到stdout流中。

结果是您stdout不包含谨慎的print调用结果,而是来自不同线程的混合输出,所有这些都混杂在一起。

解决方法是sys.stdout.write()改用;这一个原子(线程安全)操作。确保包含明确的\n换行符。

于 2013-01-06T10:47:25.947 回答