multithreading - Python threading - Is this what I need? -
i'm writing python script control various hardware parts of setup. have state want keep while other parts of script thing. think while loop stopping other parts of script running. need introduce threading that loop can continue run until tell stop?
not sure if looking @ right method solve issue.
edit: have pasted of code whole thing quite large.
this listens usb midi device messages of specific types , performs actions based on input values listed in inst
while true: b = f.read(1) s='rawdatab:'+ hex(ord(b)) print s if b == '\x90': note=true elif note: if b in inst: print "isnote:" + str( int(hex(ord(b)), 16)) noteaction(inst.get(b)) note=false if b== '\xb0': controller=true elif controller: #grab controller byte 1, value byte 2 bcount = bcount +1 cn=hex(ord(b)) #hex value of byte if bcount == 1: cntrl=cn if cntrl== '0xd': fspeedc= 1 print 'fspeed cnum' elif bcount == 2: cval=cn if fspeedc == 1: fspeed= int(cval,16) print 'fspeed=' + str(fspeed) motorcontrol('fwd',fspeed,0) print 'moving forward' print "cn:" + str(cntrl) + ", val:" + str(cval) print "bcount: " + str(bcount) bcount=0 cvalue=true elif cvalue: val=int(hex(ord(b)), 16) #print "2val:" + str(val) if val != -1 : print "do something" cvalue=false print "cn:" + str(cntrl) + ", val:" + str(val) controller=false # function guess can in infinite loop mode when want # while running still want above code monitor incoming midi bytes x=0 while true: gpio.wait_for_edge(11, gpio.falling) #prevent bounce time.sleep(0.2) x=x+1 print x if x % 2 == 0: controlapairofpins("17","1","24","0") print "forward" else: controlapairofpins("17","0","24","1") print "backward"
you can use python threading
module if want leave loop running while doing else. example of is
import time import threading def loop(n=10): while n>0: print n n -= 1 time.sleep(1) t = threading.thread(target=loop) print 'hello world' t.start()
hello world 10 9 8 7 6 5 4 3 2 1
Comments
Post a Comment