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

259
Visualizações
python how to run process in detached mode

here is a example:

from multiprocessing import Process
import time


def func():
    print('sub process is running')
    time.sleep(5)
    print('sub process finished')


if __name__ == '__main__':
    p = Process(target=func)
    p.start()
    print('done')

what I expect is that the main process will terminate right after it start a subprocess. But after printing out 'done', the terminal is still waiting....Is there any way to do this so that the main process will exit right after printing out 'done', instead of waiting for subprocess? I'm confused here because I'm not calling p.join()

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Python will not end if there exists a non-daemon process.

By setting, daemon attribute before start() call, you can make the process daemonic.

p = Process(target=func)
p.daemon = True  # <-----
p.start()
print('done')

NOTE: There will be no sub process finished message printed; because the main process will terminate sub-process at exit. This may not be what you want.

You should do double-fork:

import os
import time
from multiprocessing import Process


def func():
    if os.fork() != 0:  # <--
        return          # <--
    print('sub process is running')
    time.sleep(5)
    print('sub process finished')


if __name__ == '__main__':
    p = Process(target=func)
    p.start()
    p.join()
    print('done')
over 4 years ago · Santiago Trujillo Relatório

0

Following the excellent answer from @falsetru, I wrote out a quick generalization in the form of a decorator.

import os
from multiprocessing import Process


def detachify(func):
    """Decorate a function so that its calls are async in a detached process.

    Usage
    -----

    .. code::
            import time

            @detachify
            def f(message):
                time.sleep(5)
                print(message)

            f('Async and detached!!!')

    """
    # create a process fork and run the function
    def forkify(*args, **kwargs):
        if os.fork() != 0:
            return
        func(*args, **kwargs)

    # wrapper to run the forkified function
    def wrapper(*args, **kwargs):
        proc = Process(target=lambda: forkify(*args, **kwargs))
        proc.start()
        proc.join()
        return

    return wrapper

Usage (copied from docstring):

import time

@detachify
def f(message):
    time.sleep(5)
    print(message)

f('Async and detached!!!')

Or if you like,

def f(message):
    time.sleep(5)
    print(message)


detachify(f)('Async and detached!!!')
over 4 years ago · Santiago Trujillo Relatório
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