Python实现多线程的两种方式分析
作者:xiaofei0859 发布时间:2022-01-06 22:36:10
本文实例讲述了Python实现多线程的两种方式。分享给大家供大家参考,具体如下:
目前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()
执行结果:
starting at: 2018-08-29 09:34:16
start loop 0 at: 2018-08-29 09:34:16
start loop 1 at: 2018-08-29 09:34:16
start loop 2 at: 2018-08-29 09:34:16start loop 3 at: 2018-08-29 09:34:16start loop 4 start loopat: 52018-08-29 09:34:16
at: 2018-08-29 09:34:16
start loop 6 at: start loop2018-08-29 09:34:16
7 at: 2018-08-29 09:34:16
start loop 8 start loopat: 9 2018-08-29 09:34:16at:
2018-08-29 09:34:16
looploop 1 done at: 2018-08-29 09:34:18
0 done at: 2018-08-29 09:34:18
loop 3 done at: 2018-08-29 09:34:18
looploop 5 done at: 2018-08-29 09:34:18
2 done at: 2018-08-29 09:34:18
loop 4 done at: 2018-08-29 09:34:18
looploop 7 done at: 2018-08-29 09:34:18
6 done at: 2018-08-29 09:34:18
looploop 98 done at:done at: 2018-08-29 09:34:182018-08-29 09:34:18all Done at: 2018-08-29 09:34:18
第二种 创建继承自Thread的类
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()
执行结果:
starting at: 2018-08-29 09:36:02
start loop 0 at: Wed Aug 29 09:36:02 2018
start loop 1 at: Wed Aug 29 09:36:02 2018
start loop 2 at: Wed Aug 29 09:36:02 2018
start loop 3 at: Wed Aug 29 09:36:02 2018
start loop 4 at: Wed Aug 29 09:36:02 2018
start loop 5 at: Wed Aug 29 09:36:02 2018
start loop 6 at: Wed Aug 29 09:36:02 2018
start loop 7 at: Wed Aug 29 09:36:02 2018
start loop 8 at: Wed Aug 29 09:36:02 2018
start loop 9 at: Wed Aug 29 09:36:02 2018
loop 0 done at: Wed Aug 29 09:36:04 2018
loop 1 done at: Wed Aug 29 09:36:04 2018
looploop 23 done at:done at: Wed Aug 29 09:36:04 2018Wed Aug 29 09:36:04 2018loop 4 done at: Wed Aug 29 09:36:04 2018
loop 5 done at: Wed Aug 29 09:36:04 2018
loop 6 done at: Wed Aug 29 09:36:04 2018
looploop loop 8 9 7 done at: done at: done at: Wed Aug 29 09:36:04 2018 Wed Aug 29 09:36:04 2018
Wed Aug 29 09:36:04 2018all Done at: 2018-08-29 09:36:04
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/xiaofei0859/article/details/51316256


猜你喜欢
- 有时候我们需要查看mysql的版本信息,那么就可以参考下面的方法1、使用命令行模式进入mysql会看到最开始的提示符在命令行登录mysql,
- 我就废话不多说了,直接上代码吧!#-*- encoding:utf-8 -*-'''python 绘制颜色直方图
- 前言Redis是一个开源的内存数据库,在项目开发中redis的使用也比较频繁,本文介绍了Go语言中go-redis库的基本使用。感兴趣的小伙
- 最近一直在“深山老林”中修炼“支付宝新版收银台”,经历了白板设计,视觉设计,前端开发,前后端联调各个阶段。点点滴滴……重点谈谈对交互设计的感
- renderer是Go语言的一个简单的、轻量的、快速响应的呈现包,它可以支持JSON、JSONP、XML、HYAML、HTML、File等类
- Python开发最牛逼的IDE——pycharm(其实其它的工具,例如eclipse也可以写,只不过比较麻烦,需要安装很多的插件,所以说py
- 导言如我们在之前的教程里讨论的那样,分页可以通过两种方法来实现:1.默认分页– 你仅仅只用选中data Web control的 智能标签的
- 本程序有两文件test.asp 和tree.asp 还有一些图标文件 1。test.asp 调用类生成树 代码如下<%@
- 通过一个简单的实例,来让大家了解一下golang flag包的一个简单的用法package mainimport ( "
- queue介绍queue是python中的标准库,俗称队列。在python中,多个线程之间的数据是共享的,多个线程进行数据交换的时候,不能够
- 本文实例为大家分享了js选项卡切换的具体代码,可以手动切换,另设置定时器可使其自动切换,供大家参考,具体内容如下效果如图:具体代码:<
- 如下所示:s=subprocess.Popen("ping baidu.com -t",bufsize=0,stdout
- 最近学习Python,于是就用Python写了一个抓取Discuz!用户名的脚本,代码很少但是很搓。思路很简单,就是正则匹配title然后提
- 一、说明 关于matplotlib的scatter函数有许多活动参数,如果不专门注解,是
- #!/usr/bin/env python# -*- coding: utf-8 -*-from tkinter import *impor
- 本文实例为大家分享了python使用tkinter实现简单计算器的具体代码,供大家参考,具体内容如下class Counter: #引入t
- 1. 引言Python程序有许多模块和第三方包,这非常有助于高效编程。了解这些模块的正确使用方法是很重要的,在本文中,主要介绍一些非常实用的
- 本文实例讲述了C#简单连接sql数据库的方法。分享给大家供大家参考,具体如下:using System;using System.Colle
- 简介Python中布尔值(Booleans)表示以下两个值之一:True或False。布尔值在编程中,通常需要知道表达式是 True 还是
- 本文实例讲述了Python实现快速排序的方法。分享给大家供大家参考,具体如下:说起快排的Python实现,首先谈一下,快速排序的思路:1、取