python:threading多线程模块-创建线程
创建线程的两种方法:
1,直接调用threading.Thread来构造thread对象,Thread的参数如下:
class threading.Thread(group=None, target=None, name=None, args=(), kwargs={})
group为None;
target为线程将要执行的功能函数;
name为线程的名字,也可以在对象构造后调用setName()来设定;
args为tuple类型的参数,可以为多个,如果只有一个也的使用tuple的形式传入,例如(1,);
kwargs为dict类型的参数,也即位命名参数
threading.Thread对象的其他方法:
start(),用来启动线程;
join(), 等待直到线程结束;
isAlive(),获取线程状态
setDeamon(), 设置线程为deamon线程,必须在start()调用前调用,默认为非demon。
注意: python的主线程在没有非deamon线程存在时就会退出。
threading.currentthread() , 用来获得当前的线程;
threading.enumerate() , 用来多的当前存活的所有线程;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#coding:utf-8import threadingdef func1(num): for i in range(num): #threading.currentThread()获取当前线程,getName()获取线程名字 print 'I am %s.num:%s' % (threading.currentThread().getName(), i) def main(thread_num): thread_list = [] #定义一个线程列表 for i in range(thread_num): thread_list.append(threading.Thread(target=func1, args = (3, ))) for a in thread_list: #a.setDaemon(True)这个setDaemon默认为False 非守护线程 #表示主线程等所有子线程结束后,在结束 #设置为True的话 表示是个守护线程 子线程就会随着主线程的结束而结束 #听说服务监控工具生成的心跳线程 就是用的守护线程 a.start() for a in thread_list: a.join() #表示等待直到线程运行完毕 main(3)#########运行结果######>>> I am Thread-1.num:0I am Thread-1.num:1I am Thread-1.num:2I am Thread-2.num:0I am Thread-2.num:1I am Thread-2.num:2I am Thread-3.num:0I am Thread-3.num:1I am Thread-3.num:2 |
2,直接从threading.Thread继承,然后重写__init__方法和run方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#coding:utf-8import threadingclass MyThread(threading.Thread): #继承父类threading.Thread def __init__(self, num ): threading.Thread.__init__(self) self.num = num #把要执行的代码写到run函数里面 线程在创建后会直接运行run函数 def run(self): for i in range(self.num): print 'I am %s.num:%s' % (self.getName(), i) for i in range(3): t = MyThread(3) t.start() t.join()##########运行结果#########>>> I am Thread-1.num:0I am Thread-1.num:1I am Thread-1.num:2I am Thread-2.num:0I am Thread-2.num:1I am Thread-2.num:2I am Thread-3.num:0I am Thread-3.num:1I am Thread-3.num:2 |
本文出自 “师父领进门,修行靠个人!” 博客,请务必保留此出处http://zeping.blog.51cto.com/6140112/1258966
python:threading多线程模块-创建线程的更多相关文章
- python笔记9 线程进程 threading多线程模块 GIL锁 multiprocessing多进程模块 同步锁Lock 队列queue IO模型
线程与进程 进程 进程就是一个程序在一个数据集上的一次动态执行过程.进程一般由程序.数据集.进程控制块三部分组成.我们编写的程序用来描述进程要完成哪些功能以及如何完成:数据集则是程序在执行过程中所需要 ...
- Python使用Threading模块创建线程
使用Threading模块创建线程,直接从threading.Thread继承,然后重写__init__方法和run方法: #!/usr/bin/python # -*- coding: UTF-8 ...
- 使用threading模块创建线程
#_author:来童星#date:2019/12/17#使用threading模块创建线程import threading,timedef process(): for i in range(3): ...
- iOS开发多线程篇—创建线程
iOS开发多线程篇—创建线程 一.创建和启动线程简单说明 一个NSThread对象就代表一条线程 创建.启动线程 (1) NSThread *thread = [[NSThread alloc] in ...
- 基于Python的多线程模块Threading小结
步入正题前,先准备下基本知识,线程与进程的概念. 相信作为一个测试人员,如果从理论概念上来说其两者的概念或者区别,估计只会一脸蒙蔽,这里就举个例子来说明下其中的相关概念. 平安夜刚过,你是吃到了苹果还 ...
- threading模块创建线程
什么是线程 (thread) 线程也是一种多任务编程方式,可以使用计算机的多核资源.线程被称为轻量级的进程. 线程特征 *线程计算机多核分配的最小单位 *一个进程可以包含多个线程 *线程也是一个运行的 ...
- python-网络安全编程第六天(threading多线程模块&Queue模块&subprocess模块)
前言 昨天晚上9点多就睡了 2点起来没睡意... 那就学习吧emmmm ,拿起闲置几天的python课程学习.学习到现在5.58了 总结下 继续开始学习新的内容 多多线程? 线程(英语:thread) ...
- Python threading(多线程)
threading模块在较低级别thread模块之上构建更高级别的线程接口. 一.threading模块定义了以下函数和对象: threading.active_count() 等同于threadin ...
- java多线程之创建线程的4种方式及Future
Java使用Thread类代表线程,所有的线程对象都必须是Thread类或其子类的实例.Java可以用四种方式来创建线程: 继承Thread创建线程 实现Runnable接口创建线程 实现callab ...
随机推荐
- PHP禁止同一IP频繁访问以防止网站被防攻击或采集的代码
PHP禁止同一IP频繁访问以防止网站被防攻击或采集的代码 <?php /* *通过禁止IP频繁访问防止网站被防攻击代码*design by www.scutephp.com*/header('C ...
- 编写一个make
一.简介 How to make a "make"?在进行实现前,应该先对make有一个最基本的了解.这里稍作简介:当一个程序的源文件较少时,对其进行修改并重新生成可执行文件并不复 ...
- CentOS上安装spark standalone mode(转载)
原文链接 http://blog.csdn.net/chenxingzhen001/article/details/11072765 参考: http://spark.incubator.apache ...
- 15 个有趣的 JavaScript 与 CSS 库
原文转载:http://www.codeceo.com/article/15-interesting-js-css-framework.html 1. Wing Wing 是一个微型(压缩后仅有4KB ...
- go 静态web服务器
package main import ( "net/http" ) type helloHandler struct{} func (h *helloHandler) Serve ...
- scala学习之: Flatten a nested list structure
题目要求: (**) Flatten a nested list structure. Example: scala> flatten(List(List(1, 1), 2, List(3, L ...
- 数据库mysql优化方案
1.创建索引对于查询占主要的应用来说,索引显得尤为重要.很多时候性能问题很简单的就是因为我们忘了添加索引而造成的,或者说没有添加更为有效的索引导致.如果不加索引的话,那么查找任何哪怕只是一条特定的数据 ...
- JavaScript内置对象之数组
一.JavaScript对象之数组 1.创建数组的方式 (1)使用Array构造函数 语法:new Array() 小括号()说明: -预先知道数组要保存的项目数量 -向Array构造函数中传递数组应 ...
- db2 with ur
这几天查询DB2数据库,老遇到select * from XXX with ur, 好奇ur是什么作用,现在记录一下. DB2中,共有四种隔离级:RS,RR,CS,UR,DB2提供了这4种不同的保护级 ...
- MAGENTA: Meta-Analysis Gene-set Enrichment of variaNT Associations
MAGENTA是一款计算工具,利用全基因组遗传数据,计算预先设定的涉及生物过程或者功能性基因集在遗传相关性的富集程度.开发的目的是分析基因型不是现成的数据集,比如大型的全基因组关联荟萃分析.在以下两种 ...