0

I have some problem:

There is a about 100 threads, they doing someth, but bad piece of cake in this:

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

Please, look at this image

How can I fix it ? Or why it is happening?

  • 1
    It's pretty hard to fix bad cake. – Aesthete Sep 30 '13 at 09:28
  • 1
    possible duplicate of [How do I get a thread safe print in Python 2.6?](http://stackoverflow.com/questions/3029816/how-do-i-get-a-thread-safe-print-in-python-2-6) – Joe Sep 30 '13 at 09:28

2 Answers2

1

If you are only interested in the formatting, then I would add a newline to your print statement.

print color("HEY - Some text ... :DDD blabla - "+strftime("%X", gmtime())+"\n",fg=85)
philshem
  • 24,761
  • 8
  • 61
  • 127
0

Don't think it's possible to fix this in threads. You need one thread that will control output, your threads will send messages to this thread so that thread will handle all printing.

You can use Queue object for this (below quite modified example from documentation)

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()
Antigluk
  • 1,176
  • 2
  • 11
  • 30