zoukankan      html  css  js  c++  java
  • Python多线程下的_strptime问题

    由于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

  • 相关阅读:
    find the most comfortable road
    Rank of Tetris
    Segment set
    Codeforces Round #380 (Div. 2)D. Sea Battle
    A Bug's Life
    Is It A Tree?
    N皇后问题
    符号三角形
    2016 ICPC总结
    Sudoku Killer
  • 原文地址:https://www.cnblogs.com/dkblog/p/2840536.html
Copyright © 2011-2022 走看看