Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 811
user2827958 Avatar asked Apr 03 '14 09:04

user2827958


1 Answers

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)
like image 125
Konstantin Avatar answered Oct 01 '22 02:10

Konstantin