Advertisement
here2share

# b_threadingdecorator.py

Oct 1st, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. # b_threadingdecorator.py
  2.  
  3. # running a function in the background with threading
  4. # (apply threading to a function with a function @decorator)
  5.  
  6. import time
  7. import threading
  8. def background(f):
  9.     def bg_f(*a, **kw):
  10.         threading.Thread(target=f, args=a, kwargs=kw).start()
  11.     return bg_f
  12. #
  13. # the @decorator forces this function to run in the background
  14. @background
  15. def counter(n):
  16.     i = 0
  17.     while i < n:
  18.         print
  19.         print i
  20.         print
  21.         i += 1
  22.         time.sleep(0.5)  # 0.5 second delay
  23. counter(10)
  24. time.sleep(0.5)
  25. print 'hello'
  26. time.sleep(1.5)
  27. print 'world'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement