Skip to content

Release GIL periodically in _pickle module #78309

Open

Description

@MartinBammer
BPO 34128
Nosy @pitrou, @serhiy-storchaka, @pierreglaser
Files
  • pickle_gil.patch
  • pickle_gil.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2018-07-16.18:55:37.836>
    labels = ['interpreter-core', '3.8', 'type-feature', 'library']
    title = 'Release GIL periodically in _pickle module'
    updated_at = <Date 2019-05-10.17:46:26.367>
    user = 'https://bugs.python.org/MartinBammer'

    bugs.python.org fields:

    activity = <Date 2019-05-10.17:46:26.367>
    actor = 'pitrou'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core', 'Library (Lib)']
    creation = <Date 2018-07-16.18:55:37.836>
    creator = 'Martin Bammer'
    dependencies = []
    files = ['47700', '47701']
    hgrepos = []
    issue_num = 34128
    keywords = ['patch']
    message_count = 12.0
    messages = ['321755', '321764', '321805', '321806', '321821', '321823', '321826', '321827', '321830', '321835', '321846', '321847']
    nosy_count = 4.0
    nosy_names = ['pitrou', 'serhiy.storchaka', 'Martin Bammer', 'pierreglaser']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue34128'
    versions = ['Python 3.8']

    Activity

    1. MartinBammer commented on Jul 16, 2018

      MartinBammermannequin
      MannequinAuthor

      Hi,

      the old and slow python implementation of pickle didn't block background
      thread.
      But the newer C-implementation blocks other threads while dump/load is
      running.
      Wouldn't it be possible to allow other threads during this time?
      Especially could load/loads release the GIL, because Python objects are not available to the Python code until these functions have finished?

      Regards,
      Martin

    2. pppery commented on Jul 16, 2018

      ppperymannequin
      Mannequin

      um, something doesn't make sense about this. the python implementation of pickle never released the GIL (it can't, by definition -- it's written in python). The C implementation releasing the GIL wouldn't make sense, as the pickle api involves calls into python everywhere (for example, __reduce__)

    3. pitrou commented on Jul 17, 2018

      @pitrou
      Member

      This is about releasing the GIL periodically to allow other threads to run, as Python already does in its main interpreter loop.

    4. serhiy-storchaka commented on Jul 17, 2018

      @serhiy-storchaka
      Member

      A workaround is writing Python wrappers for IO:

      def Writer:
      def __init__(self, file):
      self.file = file
      def write(self, data):
      return self.file.write(data)

      def Reader:
      def __init__(self, file):
      self.file = file
      def read(self, size=-1):
      return self.file.read(size)
      def readline(self, size=-1):
      return self.file.readline(size)
      def peek(self, size=-1):
      return self.file.peek(size)

      def mydump(obj, file, *args, **kwargs):
          return pickle.dump(obj, Writer(file), *args, **kwargs)
      
      def myload(file, *args, **kwargs):
          return pickle.load(Reader(file), *args, **kwargs)
    5. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Jul 17, 2018
    6. changed the title [-]Do not block threads when pickle/unpickle[/-] [+]Release GIL periodically in _pickle module[/+] on Jul 17, 2018
    7. MartinBammer commented on Jul 17, 2018

      MartinBammermannequin
      MannequinAuthor

      Maybe an optional parameter with the desired interval would be good idea. So that the coder can decide if he wants/needs that feature and which interval he needs for his application.
      Otherwise it is hard to define a specific interval which fits for everyone.

    8. pitrou commented on Jul 17, 2018

      @pitrou
      Member

      The right way to do this is not to pass a timeout parameter but to check for GIL interrupts as done in the main bytecode evaluation loop.

    9. pitrou commented on Jul 17, 2018

      @pitrou
      Member

      Attaching proof-of-concept patch.

    10. pitrou commented on Jul 17, 2018

      @pitrou
      Member

      Attaching demonstration script.

    11. pitrou commented on Jul 17, 2018

      @pitrou
      Member

      (as the demo script shows, there is no detectable slowdown)

    12. serhiy-storchaka commented on Jul 17, 2018

      @serhiy-storchaka
      Member

      The demo script shows around 8% slowdown to me for

          data = list(map(float, range(N)))
    13. pitrou commented on Jul 17, 2018

      @pitrou
      Member

      Interesting, which kind of computer / system / compiler are you on?

    14. serhiy-storchaka commented on Jul 17, 2018

      @serhiy-storchaka
      Member
      CPU = Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
      Ubuntu 18.04
      Linux 4.15.0 x86_64
      gcc 7.3.0

      Performing the check in save() can have not insignificant overhead (especially after implementing the bpo-34141 optimization). It can be reduced if perform it when flush a frame (in protocol 4) or buffer to the file, or after writing significant amount of bytes into buffer.

    15. transferred this issue fromon Apr 10, 2022
    16. added and removed
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      on Oct 2, 2022
    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

      3.8 (EOL)end of lifeextension-modulesC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions