Guest User

Untitled

a guest
Jul 18th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. import time
  2. import threading
  3.  
  4. def background(f):
  5. '''
  6. a threading decorator
  7. use @background above the function you want to run in the background
  8. '''
  9. def backgrnd_func(*a, **kw):
  10. threading.Thread(target=f, args=a, kwargs=kw).start()
  11. return backgrnd_func
  12.  
  13. @background
  14. def call_function(fun_name, count):
  15. #This will print the count for every second
  16. for val in range(1, count+1):
  17. print("{} counts {}".format(fun_name, val))
  18. time.sleep(1)
  19.  
  20. # start the background function
  21. # note that with the @background decorator
  22. # background function executes simultaneously
  23.  
  24. print "My name is Amal. This is the main thread execution"
  25. counter("Background Function One", 6)
  26. counter("Background Function Two", 6)
  27. print "My name is Amal. The main thread reached here."
Add Comment
Please, Sign In to add comment