Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate KeyBoardInterrupt up to main thread. #241

Closed
calum-chamberlain opened this issue Jun 29, 2018 · 1 comment
Closed

Propagate KeyBoardInterrupt up to main thread. #241

calum-chamberlain opened this issue Jun 29, 2018 · 1 comment
Projects
Milestone

Comments

@calum-chamberlain
Copy link
Member

Is your feature request related to a problem? Please describe.

Say something is awry with you code and you want to stop it (using ctrl-C on Linux), if EQcorrscan is in a multiprocessing loop, the KeyBoardInterrupt signal only kills one working thread, and another takes its place, rather than stopping the program. It would be good to propagate this up and actually stop the program (@nackerley requested this).

Describe the solution you'd like

Reading around this shouldn't be too hard, following something like this example (copied from here:

import multiprocessing, os, time

def do_work(i):
    try:
        print 'Work Started: %d %d' % (os.getpid(), i)
        time.sleep(2)
        return 'Success'
    except KeyboardInterrupt, e:
        pass

def main():
    pool = multiprocessing.Pool(3)
    p = pool.map_async(do_work, range(6))
    try:
        results = p.get(0xFFFF)
    except KeyboardInterrupt:
        print 'parent received control-c'
        return

Describe alternatives you've considered

None, and this is really just a nicety, at the moment I kill things from htop and it is quite clumsy.

@calum-chamberlain calum-chamberlain added this to the 0.3.x milestone Jun 29, 2018
@calum-chamberlain calum-chamberlain mentioned this issue Jun 29, 2018
22 tasks
@calum-chamberlain calum-chamberlain added this to To do in 0.3.2 Jun 29, 2018
@calum-chamberlain calum-chamberlain modified the milestones: 0.3.x, 0.3.2 Jun 29, 2018
@calum-chamberlain
Copy link
Member Author

More useful Python 3 example:

import multiprocessing, os, time

def do_work(i):
    try:
        print('Work Started: {0} {1}'.format(os.getpid(), i))
        time.sleep(20)
        return 'Success'
    except KeyboardInterrupt as e:
        print("KeyBoardInterrupt on {0}".format(os.getpid()))
        pass

if __name__ == "__main__":
    pool = multiprocessing.Pool(3)
    p = pool.map_async(do_work, range(6))
    try:
        results = p.get(0xFFFF)
    except KeyboardInterrupt:
        print('parent received control-c')
        pool.terminate()
    pool.join()
    pool.close()

0.3.2 automation moved this from To do to Done Aug 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
0.3.2
  
Done
Development

No branches or pull requests

1 participant