python 多线程两种实现方式,Python多线程下的_strptime问题,
2.7版本之前python对线程的支持还不够完善,不能利用多核CPU,但是2.7版本的python中已经考虑改进这点,出现了multithreading 模块。threading模块里面主要是对一些线程的操作对象化,创建Thread的class。一般来说,使用线程有两种模式:
A 创建线程要执行的函数,把这个函数传递进Thread对象里,让它来执行;
B 继承Thread类,创建一个新的class,将要执行的代码 写到run函数里面。
本文介绍两种实现方法。
第一种 创建函数并且传入Thread 对象中
t.py 脚本内容
- import threading,time
- from time import sleep, ctime
- def now() :
- return str( time.strftime( '%Y-%m-%d %H:%M:%S' , time.localtime() ) )
- def test(nloop, nsec):
- print 'start loop', nloop, 'at:', now()
- sleep(nsec)
- print 'loop', nloop, 'done at:', now()
- def main():
- print 'starting at:',now()
- threadpool=[]
- for i in xrange(10):
- th = threading.Thread(target= test,args= (i,2))
- threadpool.append(th)
- for th in threadpool:
- th.start()
- for th in threadpool :
- threading.Thread.join( th )
- print 'all Done at:', now()
- if __name__ == '__main__':
- main()
执行结果:
thclass.py 脚本内容:
- import threading ,time
- from time import sleep, ctime
- def now() :
- return str( time.strftime( '%Y-%m-%d %H:%M:%S' , time.localtime() ) )
- class myThread (threading.Thread) :
- """docstring for myThread"""
- def __init__(self, nloop, nsec) :
- super(myThread, self).__init__()
- self.nloop = nloop
- self.nsec = nsec
- def run(self):
- print 'start loop', self.nloop, 'at:', ctime()
- sleep(self.nsec)
- print 'loop', self.nloop, 'done at:', ctime()
- def main():
- thpool=[]
- print 'starting at:',now()
- for i in xrange(10):
- thpool.append(myThread(i,2))
- for th in thpool:
- th.start()
- for th in thpool:
- th.join()
- print 'all Done at:', now()
- if __name__ == '__main__':
- main()
执行结果:
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")
python 多线程两种实现方式,Python多线程下的_strptime问题,的更多相关文章
- 【Python】python 多线程两种实现方式
目前python 提供了几种多线程实现方式 thread,threading,multithreading ,其中thread模块比较底层,而threading模块是对thread做了一些包装,可以更 ...
- Java多线程--两种实现方式
进程概述: 在这之前,有必要了解一下什么是进程? 在一个操作系统中,每个独立的执行的程序都可称为一个进程,也就是"正在运行的程序".如图所示: 线程概述: 如上所述,每个运行的程序 ...
- 快速排序quick_sort(python的两种实现方式)
排序算法有很多,目前最好的是quick_sort:unstable,spatial complexity is nlogN. 快速排序原理 python实现 严蔚敏的 datastruct书中有伪代码 ...
- Java多线程13:读写锁和两种同步方式的对比
读写锁ReentrantReadWriteLock概述 大型网站中很重要的一块内容就是数据的读写,ReentrantLock虽然具有完全互斥排他的效果(即同一时间只有一个线程正在执行lock后面的任务 ...
- 巨蟒python全栈开发数据库前端6:事件onclick的两种绑定方式&&onblur和onfocus事件&&window.onload解释&&小米商城讲解
1.回顾上节内容(JavaScript) 一.JavaScript概述 1.ECMAScript和JavaScript的关系 2.ECMAScript的历史 3.JavaScript是一门前后端都可以 ...
- python常有模块:模块、引入语法、两种执行方式、模块搜索顺序
今天主要讲了以下几点:一.模块三问.定义及分类二.import和from的语法三.文件的两种执行方式及搜索顺序四.内置函数 一.模块.import和from的语法 1.什么是模块 模块是一堆功能函 ...
- python selenium 三种等待方式详解[转]
python selenium 三种等待方式详解 引言: 当你觉得你的定位没有问题,但是却直接报了元素不可见,那你就可以考虑是不是因为程序运行太快或者页面加载太慢造成了元素不可见,那就必须要加等待 ...
- 【转载】pygame安装与两种版本的Python兼容问题
在开始学习游戏编程之前,我们先来安装下pygame和python3.2.5 参考园友: http://www.cnblogs.com/hongten/p/hongten_pygame_install. ...
- python 的几种启动方式
python 的几种启动方式 (1)利用Win的操作系统的:命令行工具 cmd.exe Win + R 调出运行对话框,然后输入cmd,即可调出“命令提示符对话框” 或者 在菜单中店家附件中的命令提 ...
随机推荐
- Myeclipse运行提示错误: 找不到或无法加载主类 test.test1 终极解决办法
前提是代码没有问题 简单粗暴的解决办法: 重启电脑 解决办法2: 1.在控制台中点开“Problems”,查看里面的错误.如果是多个项目,可以将其他项目暂时关闭. 根据错误进行处理. 2.把项目cle ...
- SDUT_2502:火星计数法
火星人的计数规则里只有a,b,c,d四个字母,计数规则从小到大是 a,b,c,d,aa,ab,ac,ad,ba,……. 给出来由a,b,c,d四种字母组成的火星数字,算出该数字是第几个(从1开始). ...
- HZOJ 太阳神
所以我刚学反演还没学反演就要做这么一道神仙题…… 首先大于n不好求,补集转化. $ans=n*n-\sum \limits _{i=1}^{n} \sum \limits _{j=1}^{n} \le ...
- c50决策树借款风险
Decision Trees/ Machine Learning Durga Gaddam August 29, 2016 Objective: The objective of the articl ...
- JavaScript for循环 while循环
循环可以将代码块执行指定的次数. JavaScript 循环 如果您希望一遍又一遍地运行相同的代码,并且每次的值都不同,那么使用循环是很方便的. 我们可以这样输出数组的值: 一般写法: documen ...
- 防止chrome主页被篡改并设置为默认打开无痕浏览方式
1. 找到chrome的快捷方式, 右击打开属性 2. 将目标框内容改为以下内容chrome.exe的目录位置 // ----- 引号中的内容为"PATH\Chrome\Applicatio ...
- HZOJ 辣鸡(ljh)
题解?noipT1还需要题解?正解就是$n^2$大暴力. 考试的时候打了$n^2$的暴力,也想到了正解的优化,然而觉得它太麻烦了,而且$n^2$怎么优化也过不了50000啊,而且即使不优化前面30分我 ...
- 为什么有时候Css样式表某个属性引用不成功?
首次使用博客,很多东西都在探索,第一篇文章也不知道发布点什么,就随便写写,是在word里面写的,也懒得排版,将就这用吧. 闲着没事找了酷狗的API写了个简单的静态网页,完成了搜索,展示,播放功能.就想 ...
- adblock自定义规则
click.admaster.cn/* cm.baidu.com/* cm.pos.baidu.com/* cpro.baidu.com/* cpro.baidustatic.com/* dup.ba ...
- Scrapy五大核心组件简介
五大核心组件 scrapy框架主要由五大组件组成,他们分别是调度器(Scheduler),下载器(Downloader),爬虫(Spider),和实体管道(Item Pipeline),Scrapy引 ...