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 6553581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:35:57+00:00 2026-05-25T12:35:57+00:00

I wonder if it is possible to shut down the communication pipe when killing

  • 0

I wonder if it is possible to shut down the communication pipe when killing a subprocess started in a different thread. If I do not call communicate() then kill() will work as expected, terminating the process after one second instead of five.

I found a discussion of a similar problem here, but I got no real answers. I assume that I either have to be able to close the pipe or to explicitly kill the sub-subprocess (which is “sleep” in the example) and kill that to unblock the pipe.

I also tried to find the answer her on SO, but I only found this and this and this, which do not directly address this problem as far as I can tell (?).

So the thing I want to do is to be able to run a command in a second thread and get all its output, but be able to kill it instantly when I so desire. I could go via a file and tail that or similar, but I think there should be a better way to do this?

import subprocess, time
from threading import Thread

process = None

def executeCommand(command, runCommand):
    Thread(target=runCommand, args=(command,)).start()

def runCommand(command):
    global process
    args = command.strip().split()
    process = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE)

    for line in process.communicate():
        if line:
            print "process:", line,

if __name__ == '__main__':
    executeCommand("./ascript.sh", runCommand)
    time.sleep(1)
    process.kill()

This is the script:

#!/bin/bash
echo "sleeping five"
sleep 5
echo "slept five"

Output

$ time python poc.py 
process: sleeping five

real    0m5.053s
user    0m0.044s
sys 0m0.000s
  • 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-25T12:35:57+00:00Added an answer on May 25, 2026 at 12:35 pm

    I think the problem is that process.kill() only kills the immediate child process (bash), not the sub-processes of the bash script.

    The problem and solution are described here:

    • http://www.doughellmann.com/PyMOTW/subprocess/#process-groups-sessions
    • How to terminate a python subprocess launched with shell=True

    Use Popen(…, preexec_fn=os.setsid) to create a process group and os.pgkill to kill the entire process group. eg

    import os
    import signal
    import subprocess
    import time
    from threading import Thread
    
    process = None
    
    def executeCommand(command, runCommand):
        Thread(target=runCommand, args=(command,)).start()
    
    def runCommand(command):
        global process
        args = command.strip().split()
        process = subprocess.Popen(
            args, shell=False, stdout=subprocess.PIPE, preexec_fn=os.setsid)
    
        for line in process.communicate():
            if line:
                print "process:", line,
    
    if __name__ == '__main__':
        executeCommand("./ascript.sh", runCommand)
        time.sleep(1)
        os.killpg(process.pid, signal.SIGKILL)
    

    $ time python poc.py 
    process: sleeping five
    
    real    0m1.051s
    user    0m0.032s
    sys 0m0.020s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this question I started to wonder: is it possible to have a
I wonder if it possible to not have jython automagicaly transform java objects to
I wonder if is possible to use FTS with LINQ using .NET Framework 3.5.
i wonder if it is possible to cascade converters when using wpf databinding. e.g.
I wonder if it is possible to create an executable module from a Python
I wonder if it is possible to use KVC to generate SQL. I'm doing
I wonder if it is possible to restrain users to insert duplicate registration records.
I wonder if this is possible with ant and java : I have a
I wonder if it is possible to use FK's in Mysql (InnoDB) for inverse
I wonder if it's possible to create an extension method which has functionality &

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.