Search code examples
pythonstrptime

Thread-safe equivalent to python's time.strptime()?


Something I wrote throws a lot of AttributeError exceptions when using time.strptime() inside a thread. This only seems to happen on Windows (not on Linux) but whatever- upon a'Googling, it seems that time.strptime() isn't considered thread-safe.

Is there a better way to create a datetime object from a string? Current code looks like:

val = DateFromTicks(mktime(strptime(val, '%B %d, %Y')))

But, that yields the exceptions as it's run inside a thread.

Thanks!


Solution

  • According to the bug report, this doesn't happen if you call strptime once before creating your threads. I've done a little testing which seems to confirm this. So just make any call to strptime during initialization as a workaround.