由于Python的datetime和time中的_strptime方法不支持多线程,运行时会报错:AttributeError: _strptime

code:

# -*- coding:utf-8 -*-
import threading
import time
import datetime ISO8601_INT_SECONDS = '%Y-%m-%dT%H:%M:%SZ'
expiry_string = "2018-01-04T04:23:02Z" def test_thread(expiry_string):
expiry = datetime.datetime.strptime(
expiry_string, ISO8601_INT_SECONDS)
print expiry threads = []
for i in xrange(5):
t = threading.Thread(target=test_thread, args=(expiry_string,))
threads.append(t) for t in threads:
t.start() for t in threads:
t.join() print "every thing is ok!!!"

会报错误:AttributeError: 'module' object has no attribute '_strptime'

 
解决方案:
1.在调用_strptime的地方加锁(推荐)
方式1.
c = threading.RLock()
def f():
    with c:
        datetime.datetime.strptime("20100101","%Y%m%d")
方式2:
LOCK = thread.allocate_lock()
 LOCK.acquire()
 datetime.datetime.strptime("20100101","%Y%m%d")
 LOCK.release()
 
2、在线程启动前调用一次_strptime(原因是报了这个错),不是很推荐
方式1、import _strptime
方式2、在调用线程前执行一次: datetime.datetime.strptime("20100101","%Y%m%d")。(似乎对我们不合适)
 
参考资料:

Python多线程下存在_strptime的问题的更多相关文章

  1. Python多线程下的_strptime问题

    Python多线程下的_strptime问题 由于Python的datetime和time中的_strptime方法不支持多线程,运行时会报错: import datetimeimport threa ...

  2. python 多线程两种实现方式,Python多线程下的_strptime问题,

    python 多线程两种实现方式 原创 Linux操作系统 作者:杨奇龙 时间:2014-06-08 20:24:26  44021  0 目前python 提供了几种多线程实现方式 thread,t ...

  3. python多线程不能利用多核cpu,但有时候多线程确实比单线程快。

    python 为什么不能利用多核 CPU  GIL 其实是因为在 python中有一个 GIL( Global Interpreter Lock),中文为:全局解释器锁.  1.最开始时候设计GIL是 ...

  4. 为什么在Python里推荐使用多进程而不是多线程?(为什么python多线程无法增加CPU使用率?)

    最近在看Python的多线程,经常我们会听到老手说:“Python下多线程是鸡肋,推荐使用多进程!”,但是为什么这么说呢? 要知其然,更要知其所以然.所以有了下面的深入研究: 首先强调背景:     ...

  5. python 多线程学习小记

    python对于thread的管理中有两个函数:join和setDaemon setDaemon:如果在程序中将子线程设置为守护线程,则该子线程会在主线程结束时自动退出,设置方式为thread.set ...

  6. python 类变量 在多线程下的共享与释放问题

    最近被多线程给坑了下,没意识到类变量在多线程下是共享的,还有一个就是没意识到 内存释放问题,导致越累越大 1.python 类变量 在多线程情况 下的 是共享的 2.python 类变量 在多线程情况 ...

  7. python多线程场景下print丢失

    python多线程情况下,print输出会出现丢失的情况,而logging模块的日志输出不会. 以下是示例代码,多运行几次就会发现这个有意思的现象 # coding:utf-8 import thre ...

  8. python 类变量 在多线程下的共享与释放问题-乾颐堂

    最近被多线程给坑了下,没意识到类变量在多线程下是共享的,还有一个就是没意识到 内存释放问题,导致越累越大 1.python 类变量 在多线程情况 下的 是共享的 2.python 类变量 在多线程情况 ...

  9. python多线程网络编程

    背景 使用过flask框架后,我对request这个全局实例非常感兴趣.它在客户端发起请求后会保存着所有的客户端数据,例如用户上传的表单或者文件等.那么在很多客户端发起请求时,服务器是怎么去区分不同的 ...

随机推荐

  1. python下载大文件

    1. wget def download_big_file_with_wget(url, target_file_name): """ 使用wget下载大文件 Note: ...

  2. Imcash平台测评报告

    ImCash是由全球知名量子基金(QuantumFund)与美国好事达保险公司 (ALL ) 联合投资美国区块链金融资本(BFC)打造全球首款量子基金数字资产服务平台 . ImCash作为全球首款量子 ...

  3. spark伪分布式的安装

    不依赖hadoop 百度分享安装包地址:http://pan.baidu.com/s/1dD4BcGT 点击打开链接 解压 并重命名: 进入spark100目录: 修改配置: Cd conf 配置单击 ...

  4. BZOJ.4299.Codechef FRBSUM(主席树)

    题目链接 记mx为最大的满足1~mx都能组成的数. 考虑当前能构成1~v中的所有数,再加入一个数x,若x>v+1,则mx=v,x不会产生影响:否则x<=v+1,则新的mx=x+v. 对于区 ...

  5. NOIP2017 d1t2 时间复杂度

    题目传送门:洛谷P3952 大模拟不解释 #include<iostream> #include<cstdio> #include<cmath> #include& ...

  6. 如何设计一个restful风格的API

    1.API接口应该尽量兼容之前的版本,在URL上应保留版本号,并同时兼容多个版本 2.每一个URI代表一个资源 3.请求方式要与http请求方式一致,GET(获取),POST(新增),PUT(更新全部 ...

  7. Nikita and string [思维-暴力] ACM

    codeforces Nikita and string time limit per test   2 seconds memory limit per test   256 megabytes O ...

  8. C#中异步调用示例与详解

    using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServi ...

  9. [LeetCode] Linked List Components 链表组件

    We are given head, the head node of a linked list containing unique integer values. We are also give ...

  10. px,em,rem的区别与用法

    别人总结的.个人觉得特别的好: http://www.w3cplus.com/css/when-to-use-em-vs-rem.html