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
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 Byecmd lineand not your interpreter like canopy , pycharm etc..printwith a function syntax suggests it is Python 3 already, so you wouldn' even have to change anything.