0

I have a script written this way on shell script

function test1(){ command1 };
function test2(){ command2 };
function test3(){ command3 };

I use the & on shell script to run those functions daemonized writting test1 & test2 & test3

But I want to do the same thing on Python. Is there a way to do it using any of python built-in functions and not using the "daemonize" library?

EDIT: I think I should have written it better. I can see now that "background" is the better word for the question. My intention was to join what I read here with the python command to make something daemonized-like. tripleee comment already answer it for me.

Thank everybody who left a comment and sorry for the mistake.

I can't give points or add a comment since I don't have enough reputation.

  • What do you mean by daemonize? Would you like them to continue to execute after the python script execution is over? Or just execute in parallel? – Ivan Nov 21 '17 at 15:56
  • 3
    Daemonizing a script is different than simply running it in the background (which is what `&` does). – chepner Nov 21 '17 at 15:58
  • You could use threads. It's not exactly the same thing but should accomplish the same goal and is as close as you're going to get using Python. – George McCollister Nov 21 '17 at 16:37

1 Answers1

0

The Python multiprocessing module allows you to run several processes simultaneously, just like the shell's background jobs.

from multiprocessing import Process

if __name__ == '__main__':
    t1 = Process(target=test1, args=('bob',))
    t1.start()
    t2 = Process(target=example2)
    t2.start()
    t3 = Process(target=demo3, args=('steve jobs', 'bill gates'))
    t3.start()
    # ... Do other stuff

... where test1, example2, and demo3 are Python functions you want to run concurrently with your main / parent process.

If you don't want to run Python code in parallel, subprocess.Popen() creates a process running an external command independently of your Python program.

tripleee
  • 175,061
  • 34
  • 275
  • 318