baronobeefdip
Posts: 76
Joined: Sat Sep 27, 2014 6:20 pm
Contact: Website

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

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

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.
Last edited by baronobeefdip on Fri Jan 22, 2016 4:19 pm, edited 1 time in total.


User avatar
Paeryn
Posts: 2966
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

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

Fri Jan 22, 2016 4:54 pm

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.
She who travels light — forgot something.

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

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

Fri Jan 22, 2016 5:02 pm

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
Last edited by Davies on Fri Jan 22, 2016 5:36 pm, edited 1 time in total.

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

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

Fri Jan 22, 2016 5:35 pm

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

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

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

Fri Jan 22, 2016 5:43 pm

That last one is not running bg_run.display in the background they're in the same thread.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

hippy
Posts: 7730
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

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

Fri Jan 22, 2016 5:45 pm

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.

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

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

Fri Jan 22, 2016 9:32 pm

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

Return to “Python”