How to close a python thread at the end of the "target" run

Asked

Viewed 19 times

0

I think I’m having problems in my python program, by the amount of Threads generated and I’m not sure how to deal with it.

Below is an excerpt of the code where N Threads are created to run the same method, with different parameters, in parallel. I am basically running the same method, N times, where N is the amount of accounts by which this method should be executed.

My question is how should I manage these Threads so that, at the end of the execution of the method (utils.processAposta), it is finalized? Or should I assume that this is done automatically after the end of the execution of the method that this Thread executes?

from threading import Thread
import encodings.idna
import time

[...]

for aposta in listApostas:
                for conta in aposta.listaContas:
                    try:
                        t = Thread(target=utils.processaAposta, args=(conta.usuario, conta.senha, aposta.cavalo, aposta.stake))
                        t.start()
                        aposta.valorTotalApostado = aposta.valorTotalApostado + aposta.stake
                        time.sleep(2)
                    except Exception as e:
                        print('ERRO NA THREAD DA CONTA: ' + conta.usuario + str(e))     
                        arqLog.write(str(e))

[...]
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.