Skip to content

Instantly share code, notes, and snippets.

@amalgjose
Last active July 18, 2018 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amalgjose/bdcd3d4d69f3b51721c1c9df412cf25f to your computer and use it in GitHub Desktop.
Save amalgjose/bdcd3d4d69f3b51721c1c9df412cf25f to your computer and use it in GitHub Desktop.
An example of execution a function in the background. The main execution will continue in the same flow without waiting for the background function to complete and the function set for background will continue its execution in the background
import time
import threading
def background(f):
'''
a threading decorator
use @background above the function you want to run in the background
'''
def backgrnd_func(*a, **kw):
threading.Thread(target=f, args=a, kwargs=kw).start()
return backgrnd_func
@background
def call_function(fun_name, count):
#This will print the count for every second
for val in range(1, count+1):
print("{} counts --> {}".format(fun_name, val))
time.sleep(1)
# start the background function
# note that with the @background decorator
# background function executes simultaneously
print "My name is Amal. This is the main thread execution"
call_function("Background Function One", 6)
call_function("Background Function Two", 6)
print "My name is Amal. The main thread reached here."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment