Home > Blockchain >  Thread stops all code for some reason - Python
Thread stops all code for some reason - Python

Time:12-22

I have a small loop that starts 5 threads, but for some reason the loop stops after it starts the first loop and not continues to start the other threads


       for i in range(5):
           
           t = threading.Thread(target=Loop.fatman(ws_server=ws_server, sessionid=sessionid, serverid=serverid, myuid=myuid, tokenn=tokenn))
           t.daemon = True
           threads.append(t)
       

       print(print("loop finished (all threads created)")

for some reason it doesn't go to the print("loop finished (all threads created)")

CodePudding user response:

I think there are 2 fundamental mistakes in your code.

  1. You have to pass a callable to the Thread target, but you are calling it in your code - t = threading.Thread(target=Loop.fatman(ws_server=ws_server, sessionid=sessionid, serverid=serverid, myuid=myuid, tokenn=tokenn)) this line actually executes the function fatman(), So a thread is not in picture, the main thread will work.
  2. You are creating your thread (even though the callable is called), but you are not starting them any where.

See the code below. It can provide some insight, may not be a fully working code - as I don't know if you have posted a complete functional unit.

_callable = Loop.fatman
kwargs = dict(ws_server=ws_server, sessionid=sessionid, serverid=serverid, myuid=myuid, tokenn=tokenn)

for i in range(5):
    t = threading.Thread(target=_callable, kwargs=kwargs)
    t.daemon = True
    # start your thread, just not create
    t.start()
    threads.append(t)

print("loop finished (all threads created)"
  • Related