Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-1348

Provide a function to safely clone a connection before or after fork()

    • Type: Icon: Improvement Improvement
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Currently, if I want to reuse a connection after fork, I need to close it before the fork.
      This is very inefficient in applications which frequently fork of subprocesses.
      Alternatively an application could also recreate the connection, but this is very hard if the component one is writing has no knowledge of how the connection was created (it's a library and just hast the db object).

      It would be very nice if it was possible to clone a connection in a fork--safe way, before or after fork.This clone method should return a closed connection so it can be used with fork!

      Currently I can restore a connection to working order after a fork in the following (technically unsupported) manner:

      connection._topology = Topology(connection._topology_settings)
      

      One could probably implement a clone() method that works similarly (deepcopy without _topology and recreating all the internal locks), then I could do:

      pid = os.fork()
      if pid == 0:
        con = con.clone()
        # Safely keep using con in the new process
      

            Assignee:
            bernie@mongodb.com Bernie Hackett
            Reporter:
            karo Karolin Varner
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: