Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 976875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:48:58+00:00 2026-05-16T03:48:58+00:00

I want to call shell commands (for example ‘sleep’ below) in parallel, report on

  • 0

I want to

  1. call shell commands (for example ‘sleep’ below) in parallel,
  2. report on their individual starts and completions and
  3. be able to kill them with ‘kill -9 parent_process_pid’.

There is already a lot written on these kinds of things already but I feel like I haven’t quite found the elegant pythonic solution I’m looking for. I’m also trying to keep things relatively readable (and short) for someone completely unfamiliar with python.

My approach so far (see code below) has been:

  1. put subprocess.call(unix_command) in a wrapper function that reports the start and completion of the command.
  2. call the wrapper function with multiprocess.Process.
  3. track the appropriate pids, store them globally, and kill them in the signal_handler.

I was trying to avoid a solution that periodically polled the processes but I’m not sure why.

Is there a better approach?

import subprocess,multiprocessing,signal
import sys,os,time

def sigterm_handler(signal, frame):
        print 'You killed me!'
        for p in pids:
                os.kill(p,9)
        sys.exit(0)

def sigint_handler(signal, frame):
        print 'You pressed Ctrl+C!'
        sys.exit(0)

signal.signal(signal.SIGINT, sigint_handler)
signal.signal(signal.SIGTERM, sigterm_handler)

def f_wrapper(d):
        print str(d) + " start"
        p=subprocess.call(["sleep","100"])
        pids.append(p.pid)
        print str(d) + " done"

print "Starting to run things."

pids=[]

for i in range(5):
        p=multiprocessing.Process(target=f_wrapper,args=(i,))
        p.daemon=True
        p.start()

print "Got things running ..."

while pids:
        print "Still working ..."
        time.sleep(1)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:48:59+00:00Added an answer on May 16, 2026 at 3:48 am

    Once subprocess.call returns, the sub-process is done — and call‘s return value is the sub-process’s returncode. So, accumulating those return codes in list pids (which btw is not synced between the multi-process appending it, and the “main” process) and sending them 9 signals “as if” they were process ids instead of return codes, is definitely wrong.

    Another thing with the question that’s definitely wrong is the spec:

    be able to kill them with ‘kill -9
    parent_process_pid’.

    since the -9 means the parent process can’t possibly intercept the signal (that’s the purpose of explicitly specifying -9) — I imagine the -9 is therefore spurious here.

    You should be using threading instead of multiprocessing (each “babysitter” thread, or process, does essentially nothing but wait for its sub-process, so why waste processes on such a lightweight task?-); you should also call suprocess.Process in the main thread (to get the sub-process started and be able to obtain its .pid to put in the list) and pass the resulting process object to the babysitter thread which waits for it (and when it’s done reports and removes it from the list). The list of subprocess ids should be guarded by a lock, since the main thread and several babysitter threads can all access it, and a set would probably be a better choice than a list (faster removals) since you don’t care about ordering nor about avoiding duplicates.

    So, roughly (no testing, so there might be bugs;-) I’d change your code to s/thing like:

    import subprocess, threading, signal
    import sys, time
    
    pobs = set()
    pobslock = threading.Lock()
    def numpobs():
        with pobslock:
            return len(pobs)
    
    def sigterm_handler(signal, frame):
        print 'You killed me!'
        with pobslock:
            for p in pobs: p.kill()
        sys.exit(0)
    
    def sigint_handler(signal, frame):
        print 'You pressed Ctrl+C!'
        sys.exit(0)
    
    signal.signal(signal.SIGINT, sigint_handler)
    signal.signal(signal.SIGTERM, sigterm_handler)
    
    def f_wrapper(d, p):
        print d, 'start', p.pid
        rc = p.wait()
        with pobslock:
            pobs.remove(p)
        print d, 'done, rc =', rc
    
    print "Starting to run things."
    
    for i in range(5):
        p = subprocess.Popen(['sleep', '100'])
        with pobslock:
            pobs.add(p)
        t = threading.Thread(target=f_wrapper, args=(i, p))
        t.daemon=True
        t.start()
    
    print "Got things running ..."
    
    while numpobs():
        print "Still working ..."
        time.sleep(1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.