The issues I noticed:
- You are replacing the
t variable in each loop. So finally you just have one thread assigned to it.
- Does the
Spawn function return a function? If it does then it's okay, otherwise you should just pass Spawn to the target, not call Spawn() .
- If the
Spawn function is long running in nature (I assume it is), then your call to the Spawn function will block the loop and wait until it returns. This is why your loop might print "looped" once and the Spawn function getting called just once too.
My suggestion would be like this:
import Handle
import threading
h = Handle.Handle()
h.StartConnection()
threads = []
for i in range(0, 5):
print("Looped")
t = threading.Thread(target=h.Spawn)
threads.append(t)
t.start()
I took a list to store the threads - the threads list. Then appending each of the thread in it before calling start. Now I can iterate over the threads list anytime I want (may be for joining them?).
Also since I assumed Spawn is a long running function, I passed it as the target to the Thread constructor. So it should be run in background when we call start on the thread. Now it should no longer block the loop.