Advertisement
here2share

# threading_simple_parallel_with_join_demo2.py

Aug 17th, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. # threading_simple_parallel_with_join_demo2.py -- Note: the threads set for 15 seconds may appear in a random order
  2. # Not sure sure why there seems to be extra linefeeds
  3.  
  4. from threading import Thread
  5. import time
  6.  
  7. def worker(order,s):
  8.     myList=[]
  9.     for i in range(s+1): # do something
  10.         myList.append(i)
  11.     time.sleep(s)
  12.     print '\n\n\nThread#%d  Seconds: %d  Current Time: %s \n\nWork: %s' % (order+1,s,time.strftime("%I:%M:%S"),myList)
  13. #
  14.  
  15. work='15 15 15 15 15 10 15 20 15 5 15 15'.split() # one 5s : nine 10s : ten 10s : one 20s
  16. print "The last thread output (#8) should appear after 20 seconds"
  17.  
  18. threads = []
  19. for i in range(len(work)):
  20.     t = Thread(target=worker, args=(i, int(work[i])))
  21.     t.start()
  22.     threads.append(t)
  23.  
  24. # join all threads
  25. for t in threads:
  26.     t.join()
  27.  
  28. print "End Of Script!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement