flefin
flefin

Reputation: 65

how do i print some string just below the loop simultaneously in python

i want to do multi process simultaneously, for example i want to print some string just below the looping underway...

import time
from threading import Thread
print 'top'
def foo():   
  for i in range(1,10):
    sys.stdout.write(str('\r%s\r'%i))
    sys.stdout.flush()
    time.sleep(1)
timer = Thread(target=foo)
timer.start()
'''bottom'

i wanna the code above will looks like this

top
'''looping counter is underway'''
bottom

Upvotes: 1

Views: 74

Answers (2)

user2665694
user2665694

Reputation:

Well, please read your favorite threading documentation.

You need to join() the thread

timer.join()

http://docs.python.org/library/threading.html#module-threading

Upvotes: 0

K. Brafford
K. Brafford

Reputation: 3849

It sounds like you want to block your main thread until your worker thread has finished. For that you want the join function

import time
import sys
from threading import Thread
print 'top'
def foo():   
  for i in range(1,10):
    sys.stdout.write(str('\r%s\r'%i))
    sys.stdout.flush()
    time.sleep(1)
timer = Thread(target=foo)
timer.start()
timer.join()
print 'bottom'

Upvotes: 0

Related Questions