Hello beautiful people..!

i need a way to run a python program from another one...

does anyone know what is the best way to call a process which runs another program in python, and when running that child process, causing the parent process to continue without waiting for the child process to return?

Dani AI

Generated

For : the typical, cross-platform way to launch a separate Python process and let the parent continue immediately is to use the subprocess API (it returns a Popen object and does not block unless wait() is called). 's point about keeping logic inside the same interpreter is valid when sharing memory/state is desirable; when an independent OS process is required, subprocess gives precise control over detachment, stdio, and startup flags.

A compact, practical pattern (works on modern Python) — run the script with the same interpreter, drop stdio to avoid tying up the caller, and use a platform-specific detach step:

import subprocess, sys, os

cmd = [sys.executable, 'child.py']

if os.name == 'nt':
    DETACHED_PROCESS = 0x00000008
    subprocess.Popen(cmd,
                     creationflags=DETACHED_PROCESS,
                     stdin=subprocess.DEVNULL,
                     stdout=subprocess.DEVNULL,
                     stderr=subprocess.DEVNULL,
                     close_fds=True)
else:
    subprocess.Popen(cmd,
                     preexec_fn=os.setsid,
                     stdin=subprocess.DEVNULL,
                     stdout=subprocess.DEVNULL,
                     stderr=subprocess.DEVNULL,
                     close_fds=True)

Notes and cautions: avoid shell=True unless a shell is actually needed (security and quoting issues). If the parent will keep running and does not collect child exit status, zombie processes can accumulate on Unix — either poll/wait in a short-lived watcher thread or handle SIGCHLD (or fully daemonize the child). For CPU-bound parallel work inside the same codebase, consider the multiprocessing module instead of threads so the work runs in separate interpreters with proper isolation and result pipes.

Summary: import-and-run (or threading) is simplest for shared code; subprocess.Popen is the right tool when an independent process is needed — use the detach flags and stdio redirection shown above, and remember to handle child reaping if the parent must stay alive.

To get two Python functions running concurrently, you're going to need to look at Python's threading modules.

There is a good site with example code here.

As for the main question, well, you could always exec() the second Python program from the first. But please don't do this unless you absolutely have to. A much neater solution is to import the second Python program as a module, then call its functions in a separate thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.