python实现多线程的两种方式


Posted in Python onMay 22, 2016

目前python 提供了几种多线程实现方式 thread,threading,multithreading ,其中thread模块比较底层,而threading模块是对thread做了一些包装,可以更加方便的被使用。
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程序设计有所帮助。

Python 相关文章推荐
python写xml文件的操作实例
Oct 05 Python
对Python新手编程过程中如何规避一些常见问题的建议
Apr 01 Python
详解JavaScript编程中的window与window.screen对象
Oct 26 Python
详解python的数字类型变量与其方法
Nov 20 Python
Python用于学习重要算法的模块pygorithm实例浅析
Aug 16 Python
Python3的介绍、安装和命令行的认识(推荐)
Oct 20 Python
对Python中list的倒序索引和切片实例讲解
Nov 15 Python
python GUI库图形界面开发之PyQt5控件QTableWidget详细使用方法与属性
Feb 25 Python
通过实例了解python__slots__使用方法
Sep 14 Python
python中scrapy处理项目数据的实例分析
Nov 22 Python
python re模块常见用法例举
Mar 01 Python
用Python远程登陆服务器的步骤
Apr 16 Python
python实现简单购物商城
May 21 #Python
python字符串的常用操作方法小结
May 21 #Python
python实现用户登录系统
May 21 #Python
python列表的常用操作方法小结
May 21 #Python
bat和python批量重命名文件的实现代码
May 19 #Python
批处理与python代码混合编程的方法
May 19 #Python
python实现汉诺塔递归算法经典案例
Mar 01 #Python
You might like
常用星际术语索引(新手指南)
2020/03/04 星际争霸
同台服务器使用缓存APC效率高于Memcached的演示代码
2010/02/16 PHP
PHP文件打开、关闭、写入的判断与执行代码
2011/05/24 PHP
php多文件上传功能实现原理及代码
2013/04/18 PHP
PHP函数preg_match_all正则表达式的基本使用详细解析
2013/08/31 PHP
php生成PDF格式文件并且加密
2015/06/22 PHP
功能强大的php分页函数
2016/07/20 PHP
PHP实现根据数组某个键值大小进行排序的方法
2018/03/13 PHP
PHPUnit测试私有属性和方法功能示例
2018/06/12 PHP
jquery异步循环获取功能实现代码
2010/09/19 Javascript
jquery点击页面任何区域实现鼠标焦点十字效果
2013/06/21 Javascript
Javascript Object 对象学习笔记
2014/12/17 Javascript
jQuery实现点击按钮弹出可关闭层的浮动层插件
2015/09/19 Javascript
JS学习之表格的排序简单实例
2016/05/16 Javascript
微信小程序 教程之条件渲染
2016/10/18 Javascript
jQuery自定义组件(导入组件)
2016/11/08 Javascript
基于JS实现仿百度百家主页的轮播图效果
2017/03/06 Javascript
浅谈AngularJS中使用$resource(已更新)
2017/09/14 Javascript
微信小程序实现团购或秒杀批量倒计时
2020/11/01 Javascript
electron中使用bootstrap的示例代码
2018/11/06 Javascript
vue中实现点击变成全屏的多种方法
2020/09/27 Javascript
[02:35]DOTA2英雄基础教程 狙击手
2014/01/14 DOTA
python多线程扫描端口示例
2014/01/16 Python
用Python编写一个国际象棋AI程序
2014/11/28 Python
Python素数检测的方法
2015/05/11 Python
Python编程之event对象的用法实例分析
2017/03/23 Python
Python元组及文件核心对象类型详解
2018/02/11 Python
关于TensorFlow新旧版本函数接口变化详解
2020/02/10 Python
Otticanet美国:最顶尖的世界名牌眼镜, 能得到打折季的价格
2019/03/10 全球购物
vue项目实现分页效果
2021/03/24 Vue.js
银行毕业实习自我鉴定
2013/09/19 职场文书
岗位聘任书范文
2014/03/29 职场文书
企业口号大全
2014/06/12 职场文书
2014年学生会主席工作总结
2014/11/07 职场文书
Go语言实现一个简单的并发聊天室的项目实战
2022/03/18 Golang
Python中文分词库jieba(结巴分词)详细使用介绍
2022/04/07 Python