Page 1 of 1

How do I get python to run a function in the background

Posted: Fri Jan 22, 2016 4:08 pm
by baronobeefdip
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

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()
and I have the file that it's importing the function that I want to run in the background looking like this

Code: Select all

import time

def display():
    print "hello from the function"
    time.sleep(1)
    display()
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.

Re: How do I get python to run a function in the background

Posted: Fri Jan 22, 2016 4:14 pm
by Massi

Re: How do I get python to run a function in the background

Posted: Fri Jan 22, 2016 4:54 pm
by Paeryn
baronobeefdip wrote:

Code: Select all

import time

def display():
    print "hello from the function"
    time.sleep(1)
    display()
Having a tail-recursive function like that isn't a good idea, I don't think python optimises it so you'll eventually run out of memory by filling the stack.

Re: How do I get python to run a function in the background

Posted: Fri Jan 22, 2016 5:02 pm
by Davies
I think your looking for something like..
file called bg_run

Code: Select all

def display():
    while 1:
        print "hello from the function"
        time.sleep(1)
file main

Code: Select all

import bg_run
t = threading.Thread(target=bg_run.display)
t.daemon = True
t.start()
and as said that tail recursive def needs to be wrapped in a while or such, not calling its self

Re: How do I get python to run a function in the background

Posted: Fri Jan 22, 2016 5:35 pm
by Davies
or even..
file name... bg_run.py

Code: Select all

import time


def display():
    print "hello from outside"
    time.sleep(1)
file main

Code: Select all

import bg_run
import time


while 1:
    bg_run.display()
    print "hello from inside"
    time.sleep(1)
of coarse this would leave a continuous loop but probably a better example than my last

Re: How do I get python to run a function in the background

Posted: Fri Jan 22, 2016 5:43 pm
by elParaguayo
That last one is not running bg_run.display in the background they're in the same thread.

Re: How do I get python to run a function in the background

Posted: Fri Jan 22, 2016 5:45 pm
by hippy

Code: Select all

#!/bin/usr/python

import threading
import time

def ThreadOne():
  while True:
    print "One"
    time.sleep(1)

def ThreadTwo():
  while True:
    print "Two"
    time.sleep(2)

def Main():
  threading.Thread(target=ThreadOne).start()
  threading.Thread(target=ThreadTwo).start()
  while True:
    print "Main"
    time.sleep(3)

if __name__ == "__main__":
  Main()
Then move "def ThreadOne()", ""def ThreadTwo()", or both, into external modules.

Re: How do I get python to run a function in the background

Posted: Fri Jan 22, 2016 9:32 pm
by Davies
elParaguayo wrote:That last one is not running bg_run.display in the background they're in the same thread.
that's true, here's something more ideal with a variable to pass between.
file.. bg_run.py

Code: Select all

import time

run = 0


def display():
    global run
    while 1:
        if run == 1:
            print "hello from outside"
            time.sleep(1)
            run = 0
main file...

Code: Select all

import bg_run
import time
import threading

t = threading.Thread(target=bg_run.display)
t.daemon = True
t.start()

while 1:
    if bg_run.run == 0:
        print "hello from inside"
        time.sleep(1)
        bg_run.run = 1