4

I have a python function that works okay in "normal" mode but throws an error when I run it in a threaded function.

Function

def is_valid_date(date_value, is_mandatory): 
    '''validate a date value. Return True if its a valid date
    http://stackoverflow.com/questions/2216250/how-can-i-validate-a-date-in-python-3-x
    '''
    try:
        if is_mandatory == True:
            if len(date_value) != 8:
                return False
            y = date_value[0:4]
            m = date_value[4:6]
            d = date_value[6:8]
            date_value = d + "/" + m + "/" + y
            date_value = time.strptime(date_value, '%d/%m/%Y') 
            return True
        else:
            if len(date_value) > 0:
                if len(date_value) != 8:
                    return False
                y = date_value[0:4]
                m = date_value[4:6]
                d = date_value[6:8]
                date_value = d + "/" + m + "/" + y
                date_value = time.strptime(date_value, '%d/%m/%Y') 
                return True
            else:
                return True     
    except ValueError:
        return False  

Error:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.2/threading.py", line 740, in _bootstrap_inner
    self.run()
  File "/home/lukik/apps/myapp/data_file/validate.py", line 97, in run
    is_valid, text_returned = is_valid_data_type(self.myText, dt_columns[colCounter].data_type, dt_columns[colCounter].mandatory)
  File "/home/lukik/apps/myapp/helper.py", line 27, in is_valid_data_type
    if is_valid_date(text_to_check, is_mandatory) != True:
  File "/home/lukik/apps/myapp/helper.py", line 91, in is_valid_date
    date_value = time.strptime(date_value, '%d/%m/%Y')
AttributeError: _strptime_time

Is it the date function that has an error or is this a "race condition" in my queuing and threading function?

lukik
  • 3,919
  • 6
  • 46
  • 89
  • Interesting, I wonder if is this a "3.2 only issue". you might use a `try:except:` phrase and print (or inspect) `time` to see if it is what you expect. – Phil Cooper Jul 30 '13 at 13:10
  • Am a python newbie. Mind explaining a bit more? – lukik Jul 30 '13 at 14:09

1 Answers1

5

Apparently there is a bug when running time.strptime() function in threaded mode in python 2.6 all the way to 3.2. I found an SO link which directed me to bugs.python.org which indicates the bug.

The hack as per @interjay on the SO link is that you require to call time.strptime() before you initialize your threads. So far it is working for me. Don't know if someone has a better solution as this feels more like a workaround than a solution.

Community
  • 1
  • 1
lukik
  • 3,919
  • 6
  • 46
  • 89