Probably the thread is started, and its surprise comes from the fact that its variable _limites
will be the even object within each thread.
You’re passing a list to a thread - the other threads will see the same list. When values are updated in the main thread, Workers will see the updated values.
If you really want to use a list for this, pass a copy of it:
from copy import copy
...
_process = threading.Thread(target=funcao, name=str(self.NUM_THREADS), args=[copy(_limites)])
Or simplesment, since there are only two values, expand the sequence "limits", so that each item turns a different parameter:
_process = threading.Thread(target=funcao, name=str(self.NUM_THREADS), args=[*_limites])
...
def processaDescricao(inicio, fim):
print("INICIADA: " + str(limites[0])+" - "+str(limites[1]))
for i in range(limites[0], limites[1]+1):
for j in palavras_chave:
requisicoes[i].eliminaPalavra(j)
requisicoes[i].eliminaCaracteresEspeciais()
Some notes unrelated to your main problem:
You will probably get an error in the size of your slice there - the behavior
Python standard, with closed slices at the beginning and open at the end (the element in the final position is not included in the slice), solves almost all problems of this type, with no need to subtract or increase "1" from the start and end of slices indexes.
In particular, this line here: _limites.append(_passo-1)
should just be _limites.append(_passo)
- and you’ll have less headache.
Second: Python has several ways to interpolate values into strings, and all of them more practical than closing a snippet of the string and using +
to concatenate the desired value.
The modern Python recommendation is to use f-strings that can expand code expressions within { }
inside the string.
That is to say:
print("INICIADA: " + str(limites[0])+" - "+str(limites[1]))
stays
print(f"INICIADA: {limites[0]} - {limites[1]}")
And finally, the use of "_" as a prefix of variable names is recommended to indicate which variables, attributes and methods are privately used by the implement of that code. But neither does it change anything in the way Python treats these variables, nor does it make sense for local variables (which are already restricted by nature). They only get in the way of the code reading, with symbols that make the brain stop to think "that this _
". (It’s a little stop and imperceptible consciously, but repeated for every variable name you see in your program while you’re working on the code - the final balance is a more tired programmer)
Who arrow the value of
self.NUM_THREADS
?– Max Fratane
self.NUM_THREADS = multiprocessing.cpu_count()
– Dan Pimentel
Dude, this isolated code seems to be working normally. https://repl.it/@Maxfratane/Oblongmarvelouspublishers
– Max Fratane
Poisé, but the first thread does not execute, starts but does not execute. It seems to be finished when the others start.
– Dan Pimentel