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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:02:10+00:00 2026-06-01T18:02:10+00:00

I need to stop the service(runs at the background in another thread) that I

  • 0

I need to stop the service(runs at the background in another thread) that I issued through Popen in python after I got the result, but the following approach failed(just use ping for the sake of explanation):

class sample(threading.Thread):
  def __init__(self, command, queue):
    threading.Thread.__init__(self)
    self.command = command;
    self.queue = queue

  def run(self):
    result = Popen(self.command, shell=True, stdout=PIPE, stderr=STDOUT)    
    while True:
      output = result.stdout.readline()
      if not self.queue.empty():
        result.kill()
        break
      if output != "": 
        print output
      else:
        break

def main():
  q = Queue()
  command = sample("ping 127.0.0.1", q)
  command.start()
  time.sleep(10)
  q.put("stop!")
  command.join()

if __name__ == "__main__":
  main()

After running above program, when I pgrep for ping, it’s still there. How can I kill the subprocess opened by Popen? Thanks.

PS: I also tried result.terminate(), but doesn’t really solve the problem either.

  • 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-06-01T18:02:11+00:00Added an answer on June 1, 2026 at 6:02 pm

    You don’t really need to run a subprocess from a thread. Try running the subprocess without a thread. Also, you specified shell=True, so it’s running the command in a shell. So there are two new processes, the shell and the command. You can also remove the shell by making shell=False.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Android service that runs in background. I need it to recieve
I've got a Windows Service that runs BackgroundWorker 's, and I'm wondering if I'm
I'm running a long simulation in MATLAB that I've realized I need to stop
When writing server-side code you need to explicitly stop execution after sending a Location:
I have submitted a task using executors and I need it to stop after
I recently got projects that runs on Struts and I am expecting more JSP
I need to create an activity that starts a service that monitors the users
I need the service to stop and be removed on its uninstall, but I
I need to stop a pair of services just before I delete a database.
I need to stop the execution Is there any procedures for using terminate and

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.