Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # b_threadingdecorator.py
- # running a function in the background with threading
- # (apply threading to a function with a function @decorator)
- import time
- import threading
- def background(f):
- def bg_f(*a, **kw):
- threading.Thread(target=f, args=a, kwargs=kw).start()
- return bg_f
- #
- # the @decorator forces this function to run in the background
- @background
- def counter(n):
- i = 0
- while i < n:
- print
- print i
- print
- i += 1
- time.sleep(0.5) # 0.5 second delay
- counter(10)
- time.sleep(0.5)
- print 'hello'
- time.sleep(1.5)
- print 'world'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement