python 线程之 threading(一)
threading:基于对象和类的较高层面上的接口,threading模块在内部使用_thread模块来实现线程的对象以及常用的同步化工具的功能。
使用定制类的方式继承 threading.Thread基类
1. 在子类中需要override __init__ 和 run() 方法。
2. 当创建这个子类的实例后调用start方法,run方法将在新的线程中的Thread框架下运行。
3. join方法的使用:等待线程退出(当在一个线程中调用另外一个线程的join方式之后,调用线程会阻塞等待调用join线程退出之后在继续运行)
4. 使用threading.Lock()创建同步锁,使用这种方式创建的锁和_thread_allocate_lock方法创建的锁相同。
import threading class MyThread(threading.Thread):
def __init__(self, myId, count, mutex):
self.myId = myId
self.count = count
self.mutex = mutex
threading.Thread.__init__(self) def run(self):
for i in range(self.count):
with self.mutex:
print('{0} id => {1}'.format(self.myId, i)) mutex = threading.Lock()
threads = [] for i in range(10):
thread = MyThread(i, 100, mutex)
threads.append(thread) for thread in threads:
thread.start()
thread.join()
print('main process existing')
使用threading模块调用线程的其他方法:
I 同上,使用Thread子类的方式开启新的线程
II 传入行为
import threading def action(num):
print("the execute num is", num) thread = threading.Thread(target=(lambda: action(2)))
thread.start()
III 传入行为,但是不使用lambda函数封装起来
threading.Thread(target=action, args=(2,)).start()
python 线程之 threading(一)的更多相关文章
- python 线程之 threading(四)
python 线程之 threading(三) http://www.cnblogs.com/someoneHan/p/6213100.html中对Event做了简单的介绍. 但是如果线程打算一遍一遍 ...
- python 线程之 threading(三)
python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...
- python 线程之threading(五)
在学习了Event和Condition两个线程同步工具之后还有一个我认为比较鸡肋的工具 semaphores 1. 使用semaphores的使用效果和Condition的notify方法的效果基本相 ...
- python 线程之 threading(二)
在http://www.cnblogs.com/someoneHan/p/6204640.html 线程一中对threading线程的开启调用做了简单的介绍 1 在线程开始之后,线程开始独立的运行直到 ...
- python 线程之_thread
python 线程之_thread _thread module: 基本用法: def child(tid): print("hello from child",tid) _thr ...
- “死锁” 与 python多线程之threading模块下的锁机制
一:死锁 在死锁之前需要先了解的概念是“可抢占资源”与“不可抢占资源”[此处的资源可以是硬件设备也可以是一组信息],因为死锁是与不可抢占资源有关的. 可抢占资源:可以从拥有他的进程中抢占而不会发生副作 ...
- python多线程之threading模块
threading模块中的对象 其中除了Thread对象以外,还有许多跟同步相关的对象 threading模块支持守护线程的机制 Thread对象 直接调用法 import threading imp ...
- python多线程之Threading
什么是线程? 线程是操作系统内核调度的基本单位,一个进程中包含一个或多个线程,同一个进程内的多个线程资源共享,线程相比进程是“轻”量级的任务,内核进行调度时效率更高. 多线程有什么优势? 多线程可以实 ...
- python多线程之threading、ThreadPoolExecutor.map
背景: 某个应用场景需要从数据库中取出几十万的数据时,需要对每个数据进行相应的操作.逐个数据处理过慢,于是考虑对数据进行分段线程处理: 方法一:使用threading模块 代码: # -*- codi ...
随机推荐
- 收集android上开源的酷炫的交互动画和视觉效果:Interactive-animation
查看网址:http://www.open-open.com/lib/view/open1411443332703.html
- yii php 图片上传与生成缩略图
今天需要做图片上传与生成缩略图的功能,把代码进行记录如下: html 视图 ($pic_action_url = $this->createAbsoluteUrl('h ...
- 使用连发互联空间+SQLyog 设置我们的数据库链接
在我使用SQLyog(小海豚)管理我的数据库的时候,主机空间为连发互联的(自己做着玩,这个便宜),遇到一些坑,自己写一下记录一下,省的下次忘记了又浪费时间. 首先你要有连发互联的空间,可以淘宝购买,连 ...
- CentOS yum的详细使用方法
yum是什么yum = Yellow dog Updater, Modified主要功能是更方便的添加/删除/更新RPM包.它能自动解决包的倚赖性问题.它能便于管理大量系统的更新问题yum特点可以同时 ...
- OpenGIS 介绍
转自:http://www.blogjava.net/sinoly/archive/2007/09/25/148002.html 值此FOSS4G大会即将召开之日,最近我会在Blog上依次介绍一些Op ...
- IPC进程通信机制
select.poll.epoll之间的区别总结[整理] 进程间通信---共享内存 信号量和互斥锁的区别 http://www.2cto.com/os/201510/445553.html http: ...
- Loadrunner中Throughput(吞吐量)的分析与计算
Throughput翻译为吞吐量,按照常规理解网络吞吐量表示在单位时间内通过网卡数据量之和,其中即包括本机网卡发送出去的数据量也包括本机网卡接收到的数据量,但这个理解在Loadrunner记录的Thr ...
- jquery学习笔记----ajax使用
一.load() 加载页面数据 load(url,[data],[callback]) url:加载的页面地址,[data]传送的数据,[callback]加载完成时回调函数. 设计一个load.ht ...
- (1)Underscore.js入门
1. Underscore对象封装 Underscore并没有在原生的JavaScript对象原型中进行扩展,而是像jQuery一样,将数据封装在一个自定义对象中(下文中称"Undersco ...
- CXF学习 (1)
Axis(Apache) -> Axis2(Apache) XFire - > CXF (XFire+Celtrix) (Apache) CXF并不仅仅是Webservice框架,更号称是 ...