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

  • Home
  • SEARCH
  • 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 8345071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:34:11+00:00 2026-06-09T06:34:11+00:00

I am using multiprocessing.Pool.map , which forks the current process. My understanding is that

  • 0

I am using multiprocessing.Pool.map, which forks the current process.

My understanding is that by default, all file descriptors including sockets are copied from the master process when forking. The master process itself is a web server (using cherrypy), so this wreaks havoc with open ports etc. The forked processes are really only doing some CPU-heavy numerical stuff inside one of the libraries that the server is using — nothing to do with the web/socket part.

Is there an easy way to automatically close all sockets in the new processes? Or another way to avoid issues with forking a CherryPy server?

Using CherryPy 3.2.2, Python 2.7; must work on Linux and OS X.

  • 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-09T06:34:12+00:00Added an answer on June 9, 2026 at 6:34 am

    POSIX does not include a sensible way to list or close a range of file descriptors.

    So we have to loop over the full range (like from 3 to 1023), closing the file descriptors one at a time.

    Or, if we have a /proc file system, we can read the list of open file descriptors in /proc/self/fd and close just those. This can be quicker than closing all possible file descriptors.

    import os
    
    def close_files(fd_min=3, fd_max=-1):
        if os.path.exists('/proc/self/fd'):
            close_files_with_procfs(fd_min, fd_max)
        else:
            close_files_exhaustively(fd_min, fd_max)
    
    def close_files_exhaustively(fd_min=3, fd_max=-1):
        import resource
        fd_top = resource.getrlimit(resource.RLIMIT_NOFILE)[1] - 1
        if fd_max == -1 or fd_max > fd_top:
            fd_max = fd_top
        for fd in range(fd_min, fd_max+1):
            try:
                os.close(fd)
            except OSError:
                pass
    
    def close_files_with_procfs(fd_min=3, fd_max=-1):
        for nm in os.listdir("/proc/self/fd"):
            if nm.startswith('.'):
                continue
            fd = int(nm)
            if fd >= fd_min and (fd_max == -1 or fd < fd_max):
                try:
                    os.close(fd)
                except OSError:
                    pass
    
    def timereps(reps, func):
        from time import time
        start = time()
        for i in range(0, reps):
            func()
        end = time()
        return (end - start) / reps
    
    print "close_files: %f" % timereps(100, lambda: close_files())
    print "close_files_exhaustively: %f" % timereps(100, lambda: close_files_exhaustively())
    print "close_files_with_procfs: %f" % timereps(1000, lambda: close_files_with_procfs())
    

    On my system:

    $ python ./close_fds.py 
    close_files: 0.000094
    close_files_exhaustively: 0.010151
    close_files_with_procfs: 0.000039
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Python multiprocessing to generate a temporary output file per process. They
I'm trying to process the contents of a tarfile using multiprocessing.Pool . I'm able
Suppose I have a python script called my_parallel_script.py that involves using multiprocessing to parallelize
I am using Python's multiprocessing module to process large numpy arrays in parallel. The
When using python-daemon , I'm creating subprocesses likeso: import multiprocessing class Worker(multiprocessing.Process): def __init__(self,
I'm trying to a parallelize an application using multiprocessing which takes in a very
I am trying to use a worker Pool in python using Process objects. Each
I am using multiprocessing module. This module is works on Queue that is its
I'm using multiprocessing , and specifically a Pool to spin off a couple of
I have a pool of worker processes (using multiprocessing.Pool ) and want to log

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.