Python Forum
how to use thread without locking up terminal
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to use thread without locking up terminal
#11
Thanks, I still haven't created the file to send data to the HID, since it is very straight forward I'm concentrating on receiving data. I'm having trouble 'filtering' HID.val I'm trying to have it print only when there is a difference in value which I am doing like this:
while 1:
    
    a = HIDs[0].val
    if a is not last:
        wait(2)
        print('init       ', a[0])
        print('curn ',len(a),   ' ', a)
        print('last ',len(last),' ', last)
        last=[]
        for i in a:
            last.append(int(a[i]))
    else:
        pass

the problem is that when I do last = a, last becomes last = HIDs[0].val as well as a =HIDs[0].val. how can I 'take a snapshot' of the values in the array a?
here is the code I am running, it is slightly different from the last one, I'm trying to add some other things(though they are easier to deal with).
main:
from Publsr import Publisher, HIDListen, HIDCtrl
import HIDUpdater
import threading
from queue import Queue

# Clean Up
from signal import signal, SIGTERM

# Save Threads
threads=[]
# Save HIDs
HIDs = []

def delpid(self):
    from time import sleep
    ## send code to turn off all 
    for i in threads:
        threads[i-1].exit()
    print('Closing')
    sleep(5)


# add Listener
def addKaki():
    HIDs.append(HIDCtrl('listener'))
    q = Queue()
    threads.append( threading.Thread(target=HIDUpdater.hid_run, args=(HIDs[-1], q, ) ))
    threads[len(threads)-1].start()



# /#####################\
# |###### Testing ######|
# \#####################/
  
addKaki()
last = []
#pub.register(HID, HID.update)
while 1:
    
    a = HIDs[0].val
    if a is not last:
        wait(2)
        print('init       ', a[0])
        print('curn ',len(a),   ' ', a)
        print('last ',len(last),' ', last)
        last=[]
        for i in a:
            last.append(int(a[i]))
    else:
        pass# Normal exit when killed
atexit.register(self.delpid)
signal(SIGTERM, lambda signum, stack_frame: exit(1))
HIDUpdater:
## simulate HID for testing ##
from Publsr import Publisher, HIDListen
from time import sleep as wait
from queue import Queue

# fake hid
def hid_run(hid, thread):
    pub = Publisher()
    #HID = HIDListen('speaker')
    pub.register(hid, hid.update)
    b = list(range(0, 64))
    a=0
    while 1:
        pub.dispatch(b)
        b[0]= a+1
        a+=1
        if a == 5:
            b[0] = 55
        wait(1)
and the publisher:
class Publisher(object):
  
    def __init__(self):
        self.subscribers = dict()
      
    def register(self, who, callback=None):
        if callback == None:
            callback = getattr(who, 'update')
        self.subscribers[who] = callback
      
    def unregister(self, who):
        del self.subscribers[who]
      
    def dispatch(self, message):
        for subscriber, callback in self.subscribers.items():
            callback(message)
      
### Script that listens to hid divice ###    
class HIDListen:
    def __init__(self, name):
        self.name = name
    def update(self, message):
        #print('{} got message "{}"'.format(self.name, message))
        global a
        a= message 
  
### User Script (Supposed to be simple for end-user)        
class HIDCtrl:
    def __init__(self, name):
        self.name = name
        self.val = list(range(0, 64))
        if self.val[0] == 55:
            print(self.val) 
    def update(self, message):
        #print('{} got message "{}"'.format(self.name, message))
        self.val = message
when I run this I get the following(with a 4 sec delay on HIDUpdater):
Quote:init 1
curn 64 [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
last 0 []

init 2
curn 64 [2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
last 64 [1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]

init 2
curn 64 [2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
last 64 [2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]

init 3
curn 64 [3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
last 64 [2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]

init 3
curn 64 [3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
last 64 [3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]

init 4
curn 64 [4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
last 64 [3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
sometimes python is printing the value despite a and b being the same which isn't supposed to happen.
Reply
#12
Lists and arrays are always passed as a reference if you do a simple assignment. But there is an easy way to solve this problem.
a = [1, 2, 3, 4]
last = list(a)
now a copy of list a is made and passed to last.
import numpy as np
a = np.array([1,2,3,4])
last = a.copy()
a copy is made of the array and then passed to last.
Reply
#13
Thanks, I added that first bit of code. I found the problem and solution, apparently 'is not' and '!=' aren't the same. lol
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020