How to kill all running processes of a ProcessPoolExecutor and keep the Futures?

I’m trying to write a method that kills all the processes spawned by a ProcessPoolExecutor, without waiting the processes to terminate their jobs (as executor.shutdown(wait=False) would do).

This is my code so far (I use devtools.debug, but you can use print)

import os
import signal
import time
from concurrent.futures import ProcessPoolExecutor
from devtools import debug

def task(x):
    time.sleep(5)
    return 42

class MyPoolExecutor(ProcessPoolExecutor):
    
    def terminate_processes(self):
        debug("Initial processes", self._processes)
        
        for pid in self._processes.keys():
            os.kill(pid, signal.SIGTERM)
        debug("Right after os.kill", self._processes)
        
        time.sleep(1)
        debug("After time.sleep(1)", self._processes)
        
        self.shutdown(wait=False)
        debug("After shutdown", self._processes)
        
        return 

if __name__ == "__main__":

    # SUBMIT
    with MyPoolExecutor() as executor:
        futures = [executor.submit(task, x) for x in range(5)]
        debug("Before terminate_process", futures)
        executor.terminate_processes()
        debug("After terminate_process", futures)
    debug(futures)

    # MAP
    with MyPoolExecutor() as executor:
        futures = executor.map(task, [x for x in range(5)])
        # debug("Before terminate_process", futures)  # ! BLOCKING
        executor.terminate_processes()
        debug("After terminate_process", futures)
    debug(futures)

When running the script, this is the output:

SUBMIT

thread.py:33 <module>
    'Before terminate_process' (str) len=24
    futures: [
        <Future at 0x10d74a380 state=running>,
        <Future at 0x10d749480 state=running>,
        <Future at 0x10d749330 state=running>,
        <Future at 0x10d7492a0 state=running>,
        <Future at 0x10d7490f0 state=running>,
    ] (list) len=5
thread.py:14 MyPoolExecutor.terminate_processes
    'Initial processes' (str) len=17
    self._processes: {
        62408: <SpawnProcess name='SpawnProcess-1' pid=62408 parent=62404 started>,
        62409: <SpawnProcess name='SpawnProcess-2' pid=62409 parent=62404 started>,
        62410: <SpawnProcess name='SpawnProcess-3' pid=62410 parent=62404 started>,
        62411: <SpawnProcess name='SpawnProcess-4' pid=62411 parent=62404 started>,
        62412: <SpawnProcess name='SpawnProcess-5' pid=62412 parent=62404 started>,
    } (dict) len=5
thread.py:18 MyPoolExecutor.terminate_processes
    'Right after os.kill' (str) len=19
    self._processes: {
        62408: <SpawnProcess name='SpawnProcess-1' pid=62408 parent=62404 started>,
        62409: <SpawnProcess name='SpawnProcess-2' pid=62409 parent=62404 started>,
        62410: <SpawnProcess name='SpawnProcess-3' pid=62410 parent=62404 started>,
        62411: <SpawnProcess name='SpawnProcess-4' pid=62411 parent=62404 started>,
        62412: <SpawnProcess name='SpawnProcess-5' pid=62412 parent=62404 started>,
    } (dict) len=5
thread.py:21 MyPoolExecutor.terminate_processes
    'After time.sleep(1)' (str) len=19
    self._processes: {
        62408: <SpawnProcess name='SpawnProcess-1' pid=62408 parent=62404 stopped exitcode=-SIGTERM>,
        62409: <SpawnProcess name='SpawnProcess-2' pid=62409 parent=62404 stopped exitcode=-SIGTERM>,
        62410: <SpawnProcess name='SpawnProcess-3' pid=62410 parent=62404 stopped exitcode=-SIGTERM>,
        62411: <SpawnProcess name='SpawnProcess-4' pid=62411 parent=62404 stopped exitcode=-SIGTERM>,
        62412: <SpawnProcess name='SpawnProcess-5' pid=62412 parent=62404 stopped exitcode=-SIGTERM>,
    } (dict) len=5
thread.py:24 MyPoolExecutor.terminate_processes
    'After shutdown' (str) len=14
    self._processes: None (NoneType)
thread.py:35 <module>
    'After terminate_process' (str) len=23
    futures: [
        <Future at 0x10d74a380 state=finished raised BrokenProcessPool>,
        <Future at 0x10d749480 state=finished raised BrokenProcessPool>,
        <Future at 0x10d749330 state=finished raised BrokenProcessPool>,
        <Future at 0x10d7492a0 state=finished raised BrokenProcessPool>,
        <Future at 0x10d7490f0 state=finished raised BrokenProcessPool>,
    ] (list) len=5
thread.py:36 <module>
    futures: [
        <Future at 0x10d74a380 state=finished raised BrokenProcessPool>,
        <Future at 0x10d749480 state=finished raised BrokenProcessPool>,
        <Future at 0x10d749330 state=finished raised BrokenProcessPool>,
        <Future at 0x10d7492a0 state=finished raised BrokenProcessPool>,
        <Future at 0x10d7490f0 state=finished raised BrokenProcessPool>,
    ] (list) len=5

MAP

thread.py:14 MyPoolExecutor.terminate_processes
    'Initial processes' (str) len=17
    self._processes: {
        62420: <SpawnProcess name='SpawnProcess-6' pid=62420 parent=62404 started>,
        62421: <SpawnProcess name='SpawnProcess-7' pid=62421 parent=62404 started>,
        62422: <SpawnProcess name='SpawnProcess-8' pid=62422 parent=62404 started>,
        62423: <SpawnProcess name='SpawnProcess-9' pid=62423 parent=62404 started>,
        62424: <SpawnProcess name='SpawnProcess-10' pid=62424 parent=62404 started>,
    } (dict) len=5
thread.py:18 MyPoolExecutor.terminate_processes
    'Right after os.kill' (str) len=19
    self._processes: {
        62420: <SpawnProcess name='SpawnProcess-6' pid=62420 parent=62404 stopped exitcode=-SIGTERM>,
        62421: <SpawnProcess name='SpawnProcess-7' pid=62421 parent=62404 stopped exitcode=-SIGTERM>,
        62422: <SpawnProcess name='SpawnProcess-8' pid=62422 parent=62404 stopped exitcode=-SIGTERM>,
        62423: <SpawnProcess name='SpawnProcess-9' pid=62423 parent=62404 stopped exitcode=-SIGTERM>,
        62424: <SpawnProcess name='SpawnProcess-10' pid=62424 parent=62404 stopped exitcode=-SIGTERM>,
    } (dict) len=5
thread.py:21 MyPoolExecutor.terminate_processes
    'After time.sleep(1)' (str) len=19
    self._processes: {
        62420: <SpawnProcess name='SpawnProcess-6' pid=62420 parent=62404 stopped exitcode=-SIGTERM>,
        62421: <SpawnProcess name='SpawnProcess-7' pid=62421 parent=62404 stopped exitcode=-SIGTERM>,
        62422: <SpawnProcess name='SpawnProcess-8' pid=62422 parent=62404 stopped exitcode=-SIGTERM>,
        62423: <SpawnProcess name='SpawnProcess-9' pid=62423 parent=62404 stopped exitcode=-SIGTERM>,
        62424: <SpawnProcess name='SpawnProcess-10' pid=62424 parent=62404 stopped exitcode=-SIGTERM>,
    } (dict) len=5
thread.py:24 MyPoolExecutor.terminate_processes
    'After shutdown' (str) len=14
    self._processes: None (NoneType)
thread.py:44 <module>
    'After terminate_process' (str) len=23
    futures: <generator object _chain_from_iterable_of_lists at 0x10d735cb0> (generator)
    !!! error pretty printing value: BrokenProcessPool('A process in the process pool was terminated abruptly while the future was running or pending.')
thread.py:45 <module>
    futures: (
    ) (generator)

I have two questions:

  • is this the right way to kill processes?
  • what is going on with Futures in the map case? Why is the BrokenProcessPool registred in Future.state for submit‘s Futures, while it’s raised by map‘s Futures?

Thank you

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật