Skip to content

Commit

Permalink
Reduce lock contention in Python strptime bug workaround anxdpanic#1072
Browse files Browse the repository at this point in the history
  • Loading branch information
MoojMidge committed Jan 22, 2025
1 parent 27d6a4f commit d0afb6f
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions resources/lib/youtube_plugin/kodion/utils/datetime_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from importlib import import_module
from re import compile as re_compile
from sys import modules
from threading import Condition, Lock
from threading import Lock

from ..exceptions import KodionException
from ..logger import Logger
Expand Down Expand Up @@ -283,30 +283,21 @@ def strptime(datetime_str, fmt=None):
try:
return datetime.strptime(datetime_str, fmt)
except TypeError:
if '_strptime' not in modules or strptime.reloading.locked():
if strptime.reloaded.acquire(False):
with strptime.loading:
if '_strptime' not in modules:
_strptime = import_module('_strptime')
modules['_strptime'] = _strptime
Logger.log_error('Python strptime bug workaround'
' - https://github.com/python/cpython/issues/71587')
strptime.reloaded.notify_all()
strptime.reloaded.release()
else:
strptime.reloaded.acquire()
while '_strptime' not in modules:
strptime.reloaded.wait()
_strptime = modules['_strptime']
strptime.reloaded.release()
else:
_strptime = modules['_strptime']

if timezone:
return _strptime._strptime_datetime(datetime, datetime_str, fmt)
return datetime(*(_strptime._strptime(datetime_str, fmt)[0][0:6]))


strptime.reloading = Lock()
strptime.reloaded = Condition(lock=strptime.reloading)
strptime.loading = Lock()


def since_epoch(dt_object=None):
Expand Down

0 comments on commit d0afb6f

Please sign in to comment.