I'm currently dealing with an issue involving a list of threads who all use ssh/telnet libs. I want at some timeout value for the main thread to instruct the threads to close all their resources, and self-terminate. Here's an example of what my code looks like
import threading
import time
import socket
threads = []
def do_this(data):
"""this function is not the implementation this code may not be valid"""
w = socket.create_connection(data, 100)
while True:
if 'admin' in w.read(256):
break
w.close
for data in data_list:
t = threading.Thread(target=do_this, args=(data,))
t.start()
threads.append(t)
end_time = time.time()+120
for t in threads:
t.join(end_time-time.time())
What I would like to do is have some way to signal the threads and modify the thread method so that it does something like this
def do_this(data):
w = socket.create_connection(data, 100)
while True:
if 'admin' in w.read(256):
break
w.close()
on signal:
w.close()
return