• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python实现多线程的两种方式

python实现多线程的两种方式

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python 多线程,python多线程编程,python多线程实例,python多线程爬虫,python多线程并发等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

目前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()

</div>

 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()
</div>

以上就是本文的全部内容吗,希望对大家学习python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Python多线程实现同步的四种方式
  • Python多线程实现同步的四种方式
  • Python 多线程实例详解
  • Python 多线程实例详解
  • Python实现的多线程http压力测试代码
  • Python线程指南详细介绍
  • Python线程指南详细介绍
  • python实现多线程抓取知乎用户
  • python 线程的暂停, 恢复, 退出详解及实例
  • 深入理解 Python 中的多线程 新手必看

相关文章

  • Python利用pyHook实现监听用户鼠标与键盘事件
  • python实现k均值算法示例(k均值聚类算法)
  • python回调函数用法实例分析
  • python处理圆角图片、圆形图片的例子
  • python实现带验证码网站的自动登陆实现代码
  • python抓取网页内容示例分享
  • Python安装第三方库及常见问题处理方法汇总
  • 利用Python的Twisted框架实现webshell密码扫描器的教程
  • Django中对数据查询结果进行排序的方法
  • Python实现定时任务

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python中的闭包总结
    • C#返回当前系统所有可用驱动器符号的方法
    • Python时间戳与时间字符串互相转换实例代码
    • python中的__init__ 、__new__、__call__小结
    • Python提取网页中超链接的方法
    • Python实现Mysql数据库连接池实例详解
    • python修改字典内key对应值的方法
    • 在Python下利用OpenCV来旋转图像的教程
    • 安装ElasticSearch搜索工具并配置Python驱动的方法
    • 在Gnumeric下使用Python脚本操作表格的教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有