Non-interfering printing from threads

Doing some threading with a python script and it spits out stuff in all kinds of orders. However, I want to print out a single “Remaining: x” at the end of each thread and then erase that line before the next print statement. So basically, I’m trying to implement a progress/status update that erases itself before the next print statement.

I have something like this:

for i in range(1,10):
   print "Something here"
   print "Remaining: x"
   sleep(5)
   sys.stdout.write("33[F")
   sys.stdout.write("33[K")

This works fine when you’re printing this out just the way it is; however, as soon as you implement threading, the “Remaining” text doesn’t get wiped out all the time and sometimes you get another “Something here” right before it wipes out the previous line.

Just need a bit of help trying to figure out the best way to get my progress/status text organized with multithreading.

Thanks.


Source: python

Leave a Reply