记python2并发场景下使用strptime的问题

1. 报错信息

AttributeError: 'module' object has no attribute '_strptime'

 

2. 报错原因

页面多个ajax请求到后台,同时调用一个公共方法,公共方法内有调用

datetime.datetime.strptime(from_date, '%Y-%m-%d')

 

3. 问题重现及解决

def parse_date1(date_str):
    """method 1: import datetime for each thread"""
    import datetime
    thread_name = threading.current_thread().getName()
    print('thread name: %s' % thread_name)
    result = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
    print('thread %s parse result is %s' % (thread_name, result))


c = threading.RLock()


def parse_date2(date_str):
    """method 2: with re-entrant lock"""
    with c:
        thread_name = threading.current_thread().getName()
        print('thread name: %s' % thread_name)
        result = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
        print('thread %s parse result is %s' % (thread_name, result))


def parse_date3(date_str):
    """method 3: import _strptime"""
    import _strptime
    thread_name = threading.current_thread().getName()
    print('thread name: %s' % thread_name)
    result = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
    print('thread %s parse result is %s' % (thread_name, result))


lock = threading.RLock()


def parse_date4(date_str):
    """method 4: import re-entrant lock, acquire and release"""
    lock.acquire()
    thread_name = threading.current_thread().getName()
    print('thread name: %s' % thread_name)
    result = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
    print('thread %s parse result is %s' % (thread_name, result))
    lock.release()


# method 5:upgrade to python3


def create_thread():
    date_str = "2020-07-16 04:23:02"
    threads = []
    for i in range(5):
        thread = threading.Thread(target=parse_date1, args=(date_str,))
        threads.append(thread)

    for t in threads:
        t.start()

    for t in threads:
        t.join()

    print('threads run complete.')


if __name__ == '__main__':
    create_thread()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Windows 平台上,strptime() 函数不是标准 C 库的一部分,因此不能直接使用。不过,你可以考虑使用第三方库来实现类似的功能,比如 strptime-for-windows 库。 strptime-for-windows 是一个基于 MIT 许可证的开源库,它提供了一个 strptime() 函数的实现,可以在 Windows 平台上使用。你可以从 GitHub 上下载并安装该库,步骤如下: 1. 下载 strptime-for-windows 库的源代码(https://github.com/HowardHinnant/strptime)。 2. 解压缩源代码压缩包,并将里面的 strptime.h 和 strptime.cpp 文件复制到你的项目中。 3. 在你的代码中包含 strptime.h 头文件,并调用 strptime() 函数即可。 下面是一个使用 strptime-for-windows 库的示例代码: ``` #include <stdio.h> #include <time.h> #include "strptime.h" int main(void) { struct tm tm; char* buf = "2022-01-01 12:00:00"; char* fmt = "%Y-%m-%d %H:%M:%S"; char* ptr = strptime(buf, fmt, &tm); if (ptr == NULL) { printf("strptime failed\n"); return 1; } printf("%d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); return 0; } ``` 在上面的代码中,我们使用strptime() 函数来将字符串日期时间转换成 struct tm 类型的日期时间。如果转换成功,函数返回指向字符串的下一个字符的指针,否则返回 NULL。如果返回值为 NULL,则表示转换失败。 需要注意的是,strptime-for-windows 库的实现可能与标准库的实现略有不同,因此在使用之前需要仔细阅读其文档,确保你的代码能够正确地运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值