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,即可调出“命令提示符对话框” 或者 在菜单中店家附件中的命令提 ...
随机推荐
- Codeforces 425A
点击打开题目链接 这题想了将近一天..越发觉得自己太弱(不过早已经习惯这样的自己了 :P)不过因为是自己独立想出来的,还是很高兴. 拿到题目,当看到数据量的时候,就感觉很明显的暴力. n×n×n×k也 ...
- (一)流--IO框架
介绍: IO(Input/Output)是计算机输入/输出的接口.java的核心库java.io提供了全方面的IO接口,包括:文件系统的操作,文件读写,标准设备输出等等 File 文件和目录类 In ...
- 小爬爬5:scrapy介绍3持久化存储
一.两种持久化存储的方式 1.基于终端指令的吃持久化存储: 特点:终端指令的持久化存储,只可以将parse方法的返回值存储到磁盘文件 因此我们需要将上一篇文章中的author和content作为返回值 ...
- hdu4310 贪心
考虑每次血口的要少 就按照一滴血多少伤害来计算.由于直接相除有小数.考虑x/y > a/b => x*b >y*a; #include<stdio.h> #inclu ...
- Directx11学习笔记【八】 龙书D3DApp的实现
原文:Directx11学习笔记[八] 龙书D3DApp的实现 directx11龙书中的初始化程序D3DApp跟我们上次写的初始化程序大体一致,只是包含了计时器的内容,而且使用了深度模板缓冲. D3 ...
- oracle一些常见的问题
对于权限审计和大部分语句,by session无效,无论指定by session/by access还是不指定,审计都自动为by access. 审计的语句级可以指定ALL,但是ALL只包括大部分语句 ...
- iOS runtime整理
iOS利用Runtime自定义控制器POP手势动画 http://www.cocoachina.com/ios/20150401/11459.html Objective C运行时(runtime) ...
- SDWebImage源码解析之SDWebImageManager的注解
http://www.cocoachina.com/ios/20150612/12118.html 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...
- 阿里云POLARDB如何助力轻松筹打造5亿用户信赖的大病筹款平台?
轻松筹首创了“大病救助”模式,帮助了众多病患在第一时间解決了医疗资金等问题,为了从源头解决了医疗资金问题.而在轻松筹这样全球5.5亿用户信赖的大病筹款平台的背后,是日益增长的各种数据.面对这样数据量所 ...
- iptables 连线追踪(Connection tracking)
「连線追蹤」:提供可用於判断包相关性的额外资讯.举例来說,一次FTP session同时需要两条分离的连線,控制与资料传输各一:用於追蹤FTP连線的扩充模组,运用对於FTP恊定的认知,从控制连線上流动 ...