How do I get python to run a function in the background
Posted: Fri Jan 22, 2016 4:08 pm
I am wanting my python program to execute a function from another file (I already know how to do that) but then moving on to the rest of the program executing the other contents of the file. I have a file that is set to run continuously (through redundancy) and I want it to run while the rest of the program goes on and continues to run. for instance, I have a program that looks like this in the main.py file
and I have the file that it's importing the function that I want to run in the background looking like this
I want the program that I am making to run that function in the background while the rest of the program continues to operate. in other words, I want it to start saying "hello from the function" which is the function that I want to run continuously, then print "hello from the inside" after one of the lines that says "hello from the function executes". I also want to be able to stop the function named display from running somewhere in the main function. Is this a complicated process? do I need to learn other things. I read that you can do this with the .start() object after importing threading but this hasn't been working. I really need help here.
Code: Select all
import threading
import time
from function import display
import threading
import time
from function import display
foreign = display()
foreign.daemon = True
def defintion():
print "enter a value"
value = input()
if value == 1:
foreign.start()
print "hello from the inside"
defintion()
Code: Select all
import time
def display():
print "hello from the function"
time.sleep(1)
display()