1

I'm trying to run a method in the background of python (a class that is constantly updated by messages from an aws server) and I'm using this as a template. Problem is, I can't get it to print('checkpoint') or print('bye'). It just keeps running the run(self). Why is that?

import threading
import time


class ThreadingExample(object):
    """ Threading example class
    The run() method will be started and it will run in the background
    until the application exits.
    """

    def __init__(self, interval=1):
        """ Constructor
        :type interval: int
        :param interval: Check interval, in seconds
        """
        self.interval = interval

        thread = threading.Thread(target=self.run, args=())
        thread.daemon = True                            # Daemonize thread
        thread.start()                                  # Start the execution

    def run(self):
        """ Method that runs forever """
        while True:
            # Do something
            print('Doing something imporant in the background')

            time.sleep(self.interval)

example = ThreadingExample()
time.sleep(3)
print('Checkpoint')
time.sleep(2)
print('Bye')

EDIT: I forgot to mention I'm using Python 2.7.6 on Ubuntu 14.04 LTS 64-bit

Rishi
  • 37
  • 1
  • 3
  • 1
    could you show your output and your expected output. It seems to work in my python 2.7 environment output `Doing something imporant in the background Doing something imporant in the background Doing something imporant in the background Checkpoint Doing something imporant in the background Doing something imporant in the background Bye` – The6thSense Jan 05 '16 at 05:57
  • My expected output is exactly what you put but instead it just keeps repeating 'Doing something important in the background' until I interupt it – Rishi Jan 05 '16 at 06:01
  • did you try running it through `cmd line` and not your interpreter like canopy , pycharm etc.. – The6thSense Jan 05 '16 at 06:05
  • You should ensure the thread started before starting your loop – smac89 Jan 05 '16 at 06:08
  • Why don't you use Python 3? Your use of `print` with a function syntax suggests it is Python 3 already, so you wouldn' even have to change anything. – Ulrich Eckhardt Jan 05 '16 at 06:11
  • Because I'm ssh'ing to an AWS and that's what they have installed. – Rishi Jan 05 '16 at 06:14
  • Possible duplicate of [Creating Threads in python](https://stackoverflow.com/questions/2905965/creating-threads-in-python) – Trevor Boyd Smith Nov 06 '17 at 18:51

2 Answers2

1

I've run your code it works fine (python34, ubuntu14)

with thread.daemon = True:

Doing something important in the background
Doing something important in the background
Doing something important in the background
Checkpoint
Doing something important in the background
Doing something important in the background
Bye

with daemon = False:

Doing something imporant in the background
Doing something important in the background
Doing something important in the background
Checkpoint
Doing something important in the background
Doing something important in the background
Bye
Doing something important in the background
Doing something important in the background
Doing something important in the background
Doing something important in the background
...
The6thSense
  • 8,103
  • 8
  • 31
  • 65
Yoav Glazner
  • 7,936
  • 1
  • 19
  • 36
0

NEVER MIND I FIXED IT

Instead of

 thread = threading.Thread(target=self.run, args=())

I had

 thread = threading.Thread(target=self.run(), args=())

I don't know why but those brackets made it run forever

Rishi
  • 37
  • 1
  • 3
  • 1
    when you write self.run() the function run is called from the current thread (and loops forever), -therefor you get stuck the in that call. no other thread will be created... – Yoav Glazner Jan 05 '16 at 07:36