Threads do not run - Python

Asked

Viewed 90 times

0

I need to allocate several threads to process a given data array. However, regardless of the number of threads I allocate, one of them is always unexecuted (usually the first). Follows code snippet:

def iniciaThreads(self, tamanho, funcao):
    _passo = int( (tamanho/self.NUM_THREADS) )
    _resto = tamanho%self.NUM_THREADS

    _process = None

    _limites = []
    _limites.append(0)
    _limites.append(_passo-1)

    for i in range(self.NUM_THREADS-1):
        _process =  threading.Thread(target=funcao, name=str(i), args=[_limites])
        _process.start()
        self.threads.append(_process)
        _limites[0] += _passo
        _limites[1] += _passo

    _limites[1] += _resto
    _process =  threading.Thread(target=funcao, name=str(self.NUM_THREADS), args=[_limites])
    _process.start()
    self.threads.append(_process)

def joinThreads(self):
    for i in self.threads:
        i.join()

Function performed by threads:

def processaDescricao(limites):
   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()
  • Who arrow the value of self.NUM_THREADS?

  • self.NUM_THREADS = multiprocessing.cpu_count()

  • Dude, this isolated code seems to be working normally. https://repl.it/@Maxfratane/Oblongmarvelouspublishers

  • Poisé, but the first thread does not execute, starts but does not execute. It seems to be finished when the others start.

1 answer

3


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)

Browser other questions tagged

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