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

The Archive Base Latest Questions

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

I have an executable file which I need to run very often, with different

  • 0

I have an executable file which I need to run very often, with different parameters. For this I wrote a small Python (2.7) wrapper, using the multiprocessing module, following the pattern given here.

My code looks like this:

try:
     logging.info("starting pool runs")
     pool.map(run_nlin, params)
     pool.close()
 except KeyboardInterrupt:
     logging.info("^C pressed")
     pool.terminate()
 except Exception, e:
     logging.info("exception caught: ", e)
     pool.terminate()
 finally:
     time.sleep(5)
     pool.join()
     logging.info("done")

My worker function is here:

class KeyboardInterruptError(Exception): pass

def run_nlin((path_config, path_log, path_nlin, update_method)):
    try:
        with open(path_log, "w") as log_:
            cmdline = [path_nlin, path_config]
            if update_method:
                cmdline += [update_method, ]
            sp.call(cmdline, stdout=log_, stderr=log_)
    except KeyboardInterrupt:
        time.sleep(5)
        raise KeyboardInterruptError()
    except:
        raise

path_config is the path to a configuration file for the binary program; in there is e.g. the date to run the program for.

When I start the wrapper, everything looks fine. However, when I press ^C, the wrapper script seems to launch an additional numproc processes from the pool before terminating. As an example, when I start the script for days 1-10, I can see in the ps aux output that two instances of the binary program are running (usually for days 1 and 3). Now, when I press ^C, the wrapper script exits, the binary programs for days 1 and 3 are gone, but there are new binary programs running for days 5 and 7.

So to me it seems as if the Pool launches another numproc processes before finally dying.

Any ideas what’s happening here, and what I can do about it?

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

    On this page, Jesse Noller, author of the multiprocessing module, shows that the correct way to handle KeyboardInterrupt is to have the subprocesses return — not reraise the exception. This allows the main process to terminate the pool.

    However, as the code below shows, the main process does not reach the except KeyboardInterrupt block until after all the tasks generated by pool.map have been run. This is why (I believe) you are seeing extra calls to your worker function, run_nlin, after Ctrl-C has been pressed.

    One possible workaround is to have all the worker functions test if a multiprocessing.Event has been set. If the event has been set, then have the worker bail out early, otherwise, go ahead with the long calculation.


    import logging
    import multiprocessing as mp
    import time
    
    logger = mp.log_to_stderr(logging.WARNING)
    
    def worker(x):
        try:
            if not terminating.is_set():
                logger.warn("Running worker({x!r})".format(x = x))
                time.sleep(3)
            else:
                logger.warn("got the message... we're terminating!")
        except KeyboardInterrupt:
            logger.warn("terminating is set")        
            terminating.set()
        return x
    
    def initializer(terminating_):
        # This places terminating in the global namespace of the worker subprocesses.
        # This allows the worker function to access `terminating` even though it is
        # not passed as an argument to the function.
        global terminating
        terminating = terminating_
    
    def main():
        terminating = mp.Event()    
        result = []
        pool = mp.Pool(initializer=initializer, initargs=(terminating, ))
        params = range(12)
        try:
             logger.warn("starting pool runs")
             result = pool.map(worker, params)
             pool.close()
        except KeyboardInterrupt:
            logger.warn("^C pressed")
            pool.terminate()
        finally:
            pool.join()
            logger.warn('done: {r}'.format(r = result))
    
    if __name__ == '__main__':
        main()
    

    Running the script yields:

    % test.py
    [WARNING/MainProcess] starting pool runs
    [WARNING/PoolWorker-1] Running worker(0)
    [WARNING/PoolWorker-2] Running worker(1)
    [WARNING/PoolWorker-3] Running worker(2)
    [WARNING/PoolWorker-4] Running worker(3)
    

    Here Ctrl-C is pressed; each of the workers sets the terminating event. We really only need one to set it, but this works despite the small inefficiency.

      C-c C-c[WARNING/PoolWorker-4] terminating is set
    [WARNING/PoolWorker-2] terminating is set
    [WARNING/PoolWorker-3] terminating is set
    [WARNING/PoolWorker-1] terminating is set
    

    Now all the other tasks queued by pool.map are run:

    [WARNING/PoolWorker-4] got the message... we're terminating!
    [WARNING/PoolWorker-2] got the message... we're terminating!
    [WARNING/PoolWorker-1] got the message... we're terminating!
    [WARNING/PoolWorker-2] got the message... we're terminating!
    [WARNING/PoolWorker-4] got the message... we're terminating!
    [WARNING/PoolWorker-2] got the message... we're terminating!
    [WARNING/PoolWorker-1] got the message... we're terminating!
    [WARNING/PoolWorker-3] got the message... we're terminating!
    

    Finally the main process reaches the except KeyboardInterrupt block.

    [WARNING/MainProcess] ^C pressed
    [WARNING/MainProcess] done: []
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an executeable (Command line which requires arguments/parameters) i need to run on
I need to run an application on the server. I have an executable file,
I have a command-line executable which I need to run from Java on Windows
I have the problem running executable .jar file. I've created a project which contains
I have to generate an executable (.exe) file from my python program. I would
I have an executable which is part of a batch process. This one executable
I have a perl script (or any executable) E which will take a file
Let's assume I have a package which calls an executable file somewhere in the
I have a small demo executable wrote in C++ that depends only on one
I develop Firefox extension with bundled executable file which should be run on browser

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.