Skip to content

Instantly share code, notes, and snippets.

@anandology
Created May 9, 2012 10:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anandology/2643620 to your computer and use it in GitHub Desktop.
Save anandology/2643620 to your computer and use it in GitHub Desktop.
Program to demonstrate a race condition in datetime module
"""I suspect that there is a race condition in datetime module.
When I ran the following code in a multi-threading application:
datetime.datetime.strptime('20120509100335', "%Y%m%d%H%M%S")
I've noticed the following error in the log.
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner
self.run()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 477, in run
self.__target(*self.__args, **self.__kwargs)
File "a.py", line 18, in f
datetime.datetime.strptime('20120509100335', "%Y%m%d%H%M%S")
AttributeError: _strptime
This behavior is not predictable. This program tries to reproduce that behavior.
"""
import sys
import threading
import datetime
def f():
print "BEGIN", threading.currentThread().getName()
for i in range(100):
datetime.datetime.strptime('20120509100335', "%Y%m%d%H%M%S")
print "END", threading.currentThread().getName()
def main():
threads = [threading.Thread(target=f) for i in range(10)]
for t in threads:
t.start()
for t in threads:
t.join()
main()
@dlindquist
Copy link

Note: this appears to be http://bugs.python.org/issue7980

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment