Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
Python3 Process.join() not actually waiting on Linux when the process is created in multi-thread

I need to put a timeout on a process that is created inside a thread, however i encountered a strange behavoir and i'm not sure how to proceed.

The following code executed on Linux produces a wierd bug where (if the number of thrads is greater than 2 (my laptop has 8 core) or the code is executed in a loop for a few times) the process.join() doesn't actually wait for the process to finish or the timeout to expire but just goes on with the next instruction.

If the same code is executed on Windows with python 3.9 it gives a circular import error in the libraries for no reason.

If it is executed with python 3.8 it works almost perfectly until like 256 threads, then gives the same stange beahvour on process.join() as in linux.

Error on windows Python 3.9: ImportError: cannot import name 'Queue' from partially initialized module 'multiprocessing.queues' (most likely due to a circular import)

Furthermore if i remove the return value from the process, so i remove the Queue. On linux the process.join() start working properly for arbitrarily large n_threads. However running the code in a loop stiil gives the error even for very small n_threads.

import random
from multiprocessing import Process, Queue
from threading import Thread

def dummy_process():
    return random.randint(1, 10)

#function to retrieve process return value
def process_returner(queue, function, args):
    queue.put(function(*args))

#function that creates the process with timeout
def execute_with_timeout(function, args, timeout=3):
    
    q = Queue()
    p1 = Process(
        target=process_returner,
        args=(q, function, args),
        name="P",
    )
    p1.start()
    p1.join(timeout=timeout) # SOMETIME IT DOES NOT WAIT FOR THE PROCESS TO FINISH
    if p1.exitcode is None:
        print(f"Oops, {p1} timeouts!")# SO IT RAISES THIS ERROR even if nowhere near 3 secods have passed
        raise TimeoutError
    p1.terminate()
    return q.get() if not q.empty() else None


#thread that just call the new process and stores the return value in the given array
def dummy_thread(result_array, index):
    try:
        result_array[index] = execute_with_timeout(dummy_process, args=())
    except TimeoutError:
        pass


def test():
    #in loop because with low n_threads as 4 the error is not so common
    for _ in range(10):
        n_threads =8
        results = [-1] * n_threads
        threads = set()
        for i in range(n_threads):
            t = Thread(target=dummy_thread, args=(results, i))
            threads.add(t)
            t.start()
        for t in threads:
            t.join()
        print(results)


if __name__ == '__main__':
    test()
over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda