线程时出现打印错误(Python)

2024-04-23 09:20:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些问题:

大约有100根线,它们在做一些事情,但这是很糟糕的一块蛋糕:

print color("HEY - Some text ... :DDD blabla - "+strftime("%X", gmtime()),fg=85)

Please, look at this image

我该怎么修?或者为什么会这样?你知道吗


Tags: textsome事情colorprint蛋糕pleaselook
2条回答

我不认为用线程来修复这个问题是可能的。你需要一个线程来控制输出,你的线程将发送消息到这个线程,这样线程将处理所有的打印。你知道吗

您可以为此使用Queue对象(下面是文档中经过修改的示例)

from threading import Thread
from Queue import Queue
from time import gmtime, strftime

def worker():
    while True:
        item = q.get()
        print item
        q.task_done()

q = Queue()
for i in range(1):
     t = Thread(target=worker)
     t.daemon = True
     t.start()

#    Somewhere in your threads
    q.put( color("HEY - Some text ... :DDD blabla - "+strftime("%X", gmtime()),fg=85) )
#   -
q.join()

如果您只对格式感兴趣,那么我会在print语句中添加一个换行符。你知道吗

print color("HEY - Some text ... :DDD blabla - "+strftime("%X", gmtime())+"\n",fg=85)

相关问题 更多 >