I am receiving ID's from a transmission, from different devices. I am placing those ID's into a list with a while. Populating the list is easy, but I would like to set a timer for if one of the transmitters stops transmitting for a time x, I would like to remove that ID from the list.
To build this list I have:
tags_list = []
while True:
#######################################################################################
#FOR CHECK IN THREAD
#######################################################################################
readable, writable, exceptional = select.select([sock], [], [], 1.0)
if readable:
data = sock.recv(1024)
tag_address = ':'.join('%02x' % ord(c) for c in data[12:6:-1])
if len(data) == 41:
if tag_address in valid_tags:
if len(tags_list) > 0:
if tag_address not in wander_tags:
tags_list.append(tag_address)
else:
print "ERROR"
But I cannot come up with a way to effectively remove an ID from the list when it is not received. Any ideas? tag_address is formatted xx:xx:xx:xx:xx:xx
Edited to remove "list" as name of the list.
Without seeing the rest of your code all I could say is you need to keep track of the last transmission time/ID somewhere, make a copy of list
(it's bad idea to remove things form a sequence you are currently iterating over) excluding any elements where time_now - last_transmission_time > timeout_value
and then replace list
with the copy.
So something like:
def clean_list(lst):
return [ele for ele in lst if ele.last_transmission - time_now < timeout_value]
while True:
ID = data[1:4]
if ID not in id_list:
id_list.append(ID)
id_list = clean_list(id_list)
I'm using dot notation for clarity and ease of use but you would have to make this work for your setup.
I'd use a dict.
import time
transmitted_IDs = {}
while True:
to_delete = []
ID = data[1:4]
if ID not in transmitted_IDs:
transmitted_IDs[ID] = time.time()
for ID, time_taken in transmitted_IDs.iteritems():
## Iterating over a dict, we get key, value pairs
## So here time_taken is transmitted_IDs[ID]
if time.time() - time_taken >= x:
to_delete.append[ID]
for ID in to_delete:
del transmitted_IDs[ID]
You can use transmitted_IDs.keys()
instead of wherever you're using list
in other places in the code now.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.