Search code examples
pythonbackgroundworker

Backgroundworker in python


I'm an inexperienced python programmer.

Is there a way to use the backgroundworker so that it starts at program startup and closes when program close?

I want it to watch a button, the button returns 1 when pressed. So while the program in running whenever button = 1 button has to do "this".

Can anyone help me with this?


Solution

  • Would make sense to start a separate thread within your main program and do anything in the background. As an example check the fairly simple code below:

    import threading
    import time
    
    #Routine that processes whatever you want as background
    def YourLedRoutine():
        while 1:
            print 'tick'
            time.sleep(1)
    
    t1 = threading.Thread(target=YourLedRoutine)
    #Background thread will finish with the main program
    t1.setDaemon(True)
    #Start YourLedRoutine() in a separate thread
    t1.start()
    #You main program imitated by sleep
    time.sleep(5)