close
文章出處

由于Python的datetime和time中的_strptime方法不支持多線程,運行時會報錯:

 

import datetime
import thread
import time

def f():
    datetime.datetime.strptime("20100101","%Y%m%d")

for _ in xrange(3):
    thread.start_new_thread(f, ())
time.sleep(3)

Unhandled exception in thread started by <function f at 0x2b52c24e66e0>
Traceback (most recent call last):
  File "test.py", line 7, in f
    datetime.datetime.strptime("20100101","%Y%m%d")
AttributeErrorUnhandled exception in thread started by <function f at 0x2b52c24e66e0>:
Traceback (most recent call last):
  File "test.py", line 7, in f
_strptime
    datetime.datetime.strptime("20100101","%Y%m%d")
AttributeError: _strptime

參考 http://bugs.python.org/issue7980

在源文件中可以fix這個bug,不過對于用戶來說,還是在使用的時候加鎖吧。。

c = threading.RLock()
def f():
    with c:
        datetime.datetime.strptime("20100101","%Y%m%d")

 

RLock參考

http://www.ourunix.org/post/335.html


不含病毒。www.avast.com
arrow
arrow
    全站熱搜

    AutoPoster 發表在 痞客邦 留言(0) 人氣()