Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
python cómo ejecutar el proceso en modo separado

aquí hay un ejemplo:

 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')

lo que espero es que el proceso principal termine justo después de iniciar un subproceso. Pero después de imprimir 'hecho', la terminal sigue esperando... ¿Hay alguna manera de hacer esto para que el proceso principal salga justo después de imprimir 'hecho', en lugar de esperar el subproceso? Estoy confundido aquí porque no estoy llamando a p.join()

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Python no terminará si existe un proceso que no sea un demonio .

Al configurar el atributo daemon antes de la llamada start() , puede hacer que el proceso sea demoníaco.

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

NOTA: No se imprimirá ningún mensaje de sub process finished ; porque el proceso principal terminará el subproceso al salir. Puede que esto no sea lo que quieres.

Deberías hacer doble bifurcación:

 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 Report

0

Siguiendo la excelente respuesta de @falsetru, escribí una generalización rápida en forma de decorador.

 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

Uso (copiado de docstring):

 import time @detachify def f(message): time.sleep(5) print(message) f('Async and detached!!!')

O si quieres,

 def f(message): time.sleep(5) print(message) detachify(f)('Async and detached!!!')
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!