agronholm / pythonfutures

Backport of the concurrent.futures package to Python 2.6 and 2.7
Other
232 stars 51 forks source link

how to kill program #68

Closed daiguadaidai closed 6 years ago

daiguadaidai commented 6 years ago

I run program but can't kill parent proccess.

code:

#-*- coding:utf-8 -*-
from concurrent import futures
import os
import sys
import signal

def test(num):
    import time

    print os.getpid()
    time.sleep(100)
    return time.ctime(), num

def handle_interrupt(signum, frame):
    print 'CAUGHT SIGINT!!!!!!!!!!!!!!!!!!!'
    sys.exit(1)

def main():

    signal.signal(signal.SIGTERM, handle_interrupt) # kill
    signal.signal(signal.SIGINT, handle_interrupt)  # Ctrl+C
    signal.signal(signal.SIGQUIT, handle_interrupt) # Ctrl+\

    with futures.ProcessPoolExecutor(max_workers=3) as executor:
        print 'id:', id(executor)
        future1 = executor.submit(test,1)
        future2 = executor.submit(test,2)
        print future1
        print future1

        print future1.result()
        print future2.result()

if __name__ == '__main__':
    main()

run and press Ctrl + C can exit program:

python mul_future.py 
id: 140637338392144
<Future at 0x7fe8a35b8ad0 state=running>
<Future at 0x7fe8a35b8ad0 state=running>
2082
2083
^C
CAUGHT SIGINT!!!!!!!!!!!!!!!!!!!
CAUGHT SIGINT!!!!!!!!!!!!!!!!!!!
CAUGHT SIGINT!!!!!!!!!!!!!!!!!!!
CAUGHT SIGINT!!!!!!!!!!!!!!!!!!!

But use kill can't exit program

session1:run program:

python mul_future.py 
id: 140243206338128
<Future at 0x7f8cdf41aad0 state=running>
<Future at 0x7f8cdf41aad0 state=running>
2091
2092

session2:kill parent proccess:

ps -ef | grep python
root      2087  1910  0 21:32 pts/0    00:00:00 python mul_future.py
root      2091  2087  0 21:32 pts/0    00:00:00 python mul_future.py
root      2092  2087  0 21:32 pts/0    00:00:00 python mul_future.py
root      2093  2087  0 21:32 pts/0    00:00:00 python mul_future.py

kill 2087

ps -ef | grep python
root      2087  1910  0 21:32 pts/0    00:00:00 python mul_future.py
root      2091  2087  0 21:32 pts/0    00:00:00 python mul_future.py
root      2092  2087  0 21:32 pts/0    00:00:00 python mul_future.py
root      2093  2087  0 21:32 pts/0    00:00:00 python mul_future.py

what should i do?

agronholm commented 6 years ago

Remove this line: signal.signal(signal.SIGTERM, handle_interrupt) Doing sys.exit(1) in a signal handler does not work the way you expect.

daiguadaidai commented 6 years ago

@agronholm I want to kill parent process, and subprocess exit yet. what should do?

if use multiprocessing. I can

import multiprocessing
import time
import signal
import os

def func(msg):
    for i in xrange(3):
        print msg
        time.sleep(1)

def term(sig_num, addtion):
    print 'terminate process %d' % os.getpid()
    try:
        pool.terminate()
    except Exception as e:
        print str(e)

pool = multiprocessing.Pool(processes=4)

if __name__ == "__main__":
    signal.signal(signal.SIGTERM, term)

    for i in xrange(10):
        msg = "hello %d" %(i)
        pool.apply_async(func, (msg, ))
    pool.close()
    pool.join()
    print "Sub-process(es) done."