You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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__)
changed the title [-]Do not block threads when pickle/unpickle[/-][+]Release GIL periodically in _pickle module[/+]on Jul 17, 2018
MartinBammer commented on Jul 17, 2018
MartinBammermannequin
MannequinAuthor
More actions
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.
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.
Activity
MartinBammer commentedon Jul 16, 2018
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
pppery commentedon Jul 16, 2018
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__)pitrou commentedon Jul 17, 2018
This is about releasing the GIL periodically to allow other threads to run, as Python already does in its main interpreter loop.
serhiy-storchaka commentedon Jul 17, 2018
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)
[-]Do not block threads when pickle/unpickle[/-][+]Release GIL periodically in _pickle module[/+]MartinBammer commentedon Jul 17, 2018
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.
pitrou commentedon Jul 17, 2018
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.
pitrou commentedon Jul 17, 2018
Attaching proof-of-concept patch.
pitrou commentedon Jul 17, 2018
Attaching demonstration script.
pitrou commentedon Jul 17, 2018
(as the demo script shows, there is no detectable slowdown)
serhiy-storchaka commentedon Jul 17, 2018
The demo script shows around 8% slowdown to me for
pitrou commentedon Jul 17, 2018
Interesting, which kind of computer / system / compiler are you on?
serhiy-storchaka commentedon Jul 17, 2018
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.