Skip to content

Add terminate_workers to ProcessPoolExecutor #128041

Description

@csm10495

Feature or enhancement

Proposal:

This is an interpretation of the feature ask in: https://discuss.python.org/t/cancel-running-work-in-processpoolexecutor/58605/1. It would be a way to stop all the workers running in a ProcessPoolExecutor

p = ProcessPoolExecutor()
# use p

# i know i want p to die at this point no matter what
p.terminate_workers()

Previously the way to do this was to use to loop through the ._processes of the ProcessPoolExecutor though, preferably this should be possible without accessing implementation details.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/cancel-running-work-in-processpoolexecutor/58605/1

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Dec 17, 2024
  2. added a commit that references this issue on Dec 17, 2024
  3. added a commit that references this issue on Mar 3, 2025
  4. gpshead commented on Mar 3, 2025

    @gpshead
    Member

    thanks for the feature contribution!

  5. picnixz commented on Mar 3, 2025

    @picnixz
    Member

    Re-opening because we have some build bots failing (I'm not sure whether the macOS failure is related but the other seems so)

    https://github.com/python/cpython/actions/runs/13631446353/job/38099943353#step:11:686
    https://buildbot.python.org/#/builders/568/builds/8398

  6. reopened this on Mar 3, 2025
  7. csm10495 commented on Mar 3, 2025

    @csm10495
    ContributorAuthor

    Opened #130812 to try to fix the transiency. I can't seem to add the skip-news label, but it should get that.

  8. added 2 commits that reference this issue on Mar 4, 2025
  9. colesbury commented on Mar 4, 2025

    @colesbury
    Contributor

    @csm10495 - I reverted #128043 due to CI failures. Please open a new PR with the changes and fixes when you have a chance. I can help with testing if you'd like.

  10. csm10495 commented on Mar 4, 2025

    @csm10495
    ContributorAuthor

    Thanks! @colesbury. I'll dig more to figure out the issue before opening a fresh pr.

  11. removed
    3.14bugs and security fixes
    on Mar 4, 2025
  12. csm10495 commented on Mar 4, 2025

    @csm10495
    ContributorAuthor

    Current issue:

    test_force_shutdown_workers_stops_pool (test.test_concurrent_futures.test_process_pool.ProcessPoolForkserverProcessPoolExecutorTest.test_force_shutdown_workers_stops_pool) ... 0.07s Warning -- reap_children() reaped child process 33328
    ...
    
    0:00:09 load avg: 2.32 [1/1/1] test.test_concurrent_futures.test_process_pool failed (env changed)
    
    == Tests result: ENV CHANGED ==
    
    1 test altered the execution environment (env changed):
        test.test_concurrent_futures.test_process_pool
    
    Total duration: 9.0 sec
    Total tests: run=84 skipped=9
    Total test files: run=1/1 env_changed=1
    Result: ENV CHANGED
    

    Interestingly enough it was suggested that commenting out

                self.assertRaises(RuntimeError, executor.submit, time.sleep, 0)
    

    makes the test pass. It does.. most of the time. It still transiently fails the same way with a leaked child process (though much rarer).

    So I guess the timing changed caused by that line make it more likely to happen, but we're still dealing with a race condition.

  13. csm10495 commented on Mar 4, 2025

    @csm10495
    ContributorAuthor

    I think I figured it out. Will open a PR in a few minutes.

  14. added a commit that references this issue on Mar 4, 2025
  15. csm10495 commented on Mar 4, 2025

    @csm10495
    ContributorAuthor

    Opened a new PR: #130849. I believe the transient issues are resolved.

  16. gpshead commented on Mar 4, 2025

    @gpshead
    Member

    FWIW on the original PR while I suspected we might have flakiness issues... I was way underestimating that apparently. I should've does the buildbot tests before merging. sorry for the churn! :)

  17. added a commit that references this issue on Mar 5, 2025
  18. gpshead commented on Mar 5, 2025

    @gpshead
    Member

    merged again with the improvements. 🤞🏾

  19. csm10495 commented on Mar 5, 2025

    @csm10495
    ContributorAuthor

    Tiny related fix: #130900 (docs only)

  20. added a commit that references this issue on Mar 5, 2025
  21. added 2 commits that reference this issue on Mar 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-multiprocessingtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions