【Python】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()
执行结果:
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 多线程两种实现方式,Python多线程下的_strptime问题,
python 多线程两种实现方式 原创 Linux操作系统 作者:杨奇龙 时间:2014-06-08 20:24:26 44021 0 目前python 提供了几种多线程实现方式 thread,t ...
- python 多进程的两种创建方式
Python中使用线程有两种方式:函数或者用类来包装线程对象. 第一种---------函数 菜鸟教程的我没看懂,说说我自己懂的----看视频理解的 import time import thread ...
- Python基础:Python运行的两种基本方式
完成Python的安装之后,我们可以开始编写Python代码以及运行Python程序了.我们来看一下运行Python具体有哪几种方式 1.REPL 所谓REPL即read.eva.print.loop ...
- Java多线程--两种实现方式
进程概述: 在这之前,有必要了解一下什么是进程? 在一个操作系统中,每个独立的执行的程序都可称为一个进程,也就是"正在运行的程序".如图所示: 线程概述: 如上所述,每个运行的程序 ...
- python package 的两种组织方式
方式一/package1/ .../__init__.py # 空文件 .../class1.py class Class1: def __init__(self): self.name = &quo ...
- python类的两种创建方式
参考: https://blog.csdn.net/likunkun__/article/details/81949479
- 基础知识:编程语言介绍、Python介绍、Python解释器安装、运行Python解释器的两种方式、变量、数据类型基本使用
2018年3月19日 今日学习内容: 1.编程语言的介绍 2.Python介绍 3.安装Python解释器(多版本共存) 4.运行Python解释器程序两种方式.(交互式与命令行式)(♥♥♥♥♥) 5 ...
- 周一02.3运行python程序的两种方式
一.运行python程序的两种方式 方法一:交互式: 优点:输入一行代码立刻返回结果 缺点:无法永久保存代码 方法二: ...
- 执行python解释器的两种方式
执行python解释器的两种方式 1.交互式 python是高级语言,是解释型语言,逐行翻译,写一句翻译一句 print ('hello world') 2.命令行式 python和python解释器 ...
随机推荐
- Android 之 AlertDialog 用户登录
1:activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi ...
- Altium Designer6打印PCB转印纸设置方法
在学校经常要用热转印法做一些简单的PCB板,自己用的AD6.6,学校的打印店用99的比较多,这就造成打印不方便.为了充分利用自己实验室的打印机,今天买回来两张PCB打印纸.发现用AD6打印同比例的PC ...
- QT中异形窗口的绘制(winEvent处理WM_NCHITTEST消息)
这里讨论的只是Windows平台上的实现. 在QT中绘制异形窗口,只要设定 windowFlag 为 CustomizeWindowHint,再结合setMask()就可以做出各种奇形怪状的窗口.相对 ...
- JavaScript方法undefined/null原因探究及闭包简单实现
昨天一个刚写前端不久的同学发消息问这个问题(如下图): HTML代码为(省略部分代码): <head> <script src="test.js">< ...
- HDU_2028——求多个数的最小公倍数
Problem Description 求n个数的最小公倍数. Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数. Output 为每组测试数据输出它们的最 ...
- Codeforces Round #236 (Div. 2)E. Strictly Positive Matrix(402E)
E. Strictly Positive Matrix You have matrix a of size n × n. Let's number the rows of the matrix f ...
- HDU3727--Jewel (主席树 静态区间第k大)
Jewel Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- [LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路
33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you be ...
- redis报错
网站登录异常,redis数据不能写!解决方法汇总! redis---flushdb ###提示如下错误 ###flushall 清空说有数据,所有库 (error) ...
- RequireJS入门(三)
这篇来写一个具有依赖的事件模块event.event提供三个方法bind.unbind.trigger来管理DOM元素事件. event依赖于cache模块,cache模块类似于jQuery的$.da ...