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

  • Home
  • SEARCH
  • 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 9236851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:23:52+00:00 2026-06-18T07:23:52+00:00

So I am using subprocess to spawn a long running process through the web

  • 0

So I am using subprocess to spawn a long running process through the web interface using Django. Now if a user wants to come back to the page I would like to give him the option of terminating the subprocess at a later stage.

How can do this? I implemented the same thing in Java and made a global singleton ProcessManager dictionary to store the Process Object in Memory. Can I do something similar in Python?

EDIT

Yes Singletons and a hash of ProcessManager is the way of doing it cleanly. Emmanuel’s code works perfectly fine with a few modifications.

Thanks

  • 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-18T07:23:54+00:00Added an answer on June 18, 2026 at 7:23 am

    I think an easy way to implement Singleton pattern in python is via class attributes:

    import subprocess
    
    class ProcessManager(object):
        __PROCESS = None;
    
        @staticmethod
        def set_process(args):
            # Sets singleton process
            if __PROCESS is None:
                p = subprocess.Popen(args)
                ProcessManager.__PROCESS = p;
            # else: exception handling
    
        @staticmethod
        def kill_process():
            # Kills process
            if __PROCESS is None:
                # exception handling
            else:
                ProcessManager.__PROCESS.kill()
    

    Then you can use this class via:

    from my_module import ProcessManager
    
    my_args = ...
    ProcessManager.set_process(my_args)
    
    ...
    
    ProcessManager.kill_process()
    

    Notes:

    • the ProcessManager is in charge of creating the process, to be symmetrical with its ending
    • I don’t have enough knowledge in multi-threading to know if this works in multi-threading mode
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a long running, daemonized Python process that uses subprocess to spawn new
I am using the subprocess module to find out if a process is running.
I create a subprocess using subprocess.Popen() that runs for a long time. It is
In my GUI application, I'm using the C# Process class to spawn external processes
I am using subprocess module for running commands from another app I know that
How can I get the output of a process run using subprocess.call() ? Passing
I have a small python script which invokes an external process using subprocess .
I'm controlling a child process using pexpect (because subprocess doesn't support pty's and I
I am trying to spawn a process using Popen and send it a particular
I have a Python program (precisely, a Django application) that starts a subprocess using

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.