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(一)的更多相关文章

  1. python 线程之 threading(四)

    python 线程之 threading(三) http://www.cnblogs.com/someoneHan/p/6213100.html中对Event做了简单的介绍. 但是如果线程打算一遍一遍 ...

  2. python 线程之 threading(三)

    python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...

  3. python 线程之threading(五)

    在学习了Event和Condition两个线程同步工具之后还有一个我认为比较鸡肋的工具 semaphores 1. 使用semaphores的使用效果和Condition的notify方法的效果基本相 ...

  4. python 线程之 threading(二)

    在http://www.cnblogs.com/someoneHan/p/6204640.html 线程一中对threading线程的开启调用做了简单的介绍 1 在线程开始之后,线程开始独立的运行直到 ...

  5. python 线程之_thread

    python 线程之_thread _thread module: 基本用法: def child(tid): print("hello from child",tid) _thr ...

  6. “死锁” 与 python多线程之threading模块下的锁机制

    一:死锁 在死锁之前需要先了解的概念是“可抢占资源”与“不可抢占资源”[此处的资源可以是硬件设备也可以是一组信息],因为死锁是与不可抢占资源有关的. 可抢占资源:可以从拥有他的进程中抢占而不会发生副作 ...

  7. python多线程之threading模块

    threading模块中的对象 其中除了Thread对象以外,还有许多跟同步相关的对象 threading模块支持守护线程的机制 Thread对象 直接调用法 import threading imp ...

  8. python多线程之Threading

    什么是线程? 线程是操作系统内核调度的基本单位,一个进程中包含一个或多个线程,同一个进程内的多个线程资源共享,线程相比进程是“轻”量级的任务,内核进行调度时效率更高. 多线程有什么优势? 多线程可以实 ...

  9. python多线程之threading、ThreadPoolExecutor.map

    背景: 某个应用场景需要从数据库中取出几十万的数据时,需要对每个数据进行相应的操作.逐个数据处理过慢,于是考虑对数据进行分段线程处理: 方法一:使用threading模块 代码: # -*- codi ...

随机推荐

  1. Android Multimedia框架总结(二)MediaPlayer框架及播放网络视频案例

    前言:前面一篇我们介绍MediaPlayer相关方法,有人说,没有实际例子,看得不是很明白,今天在分析MediaPlayer时,顺带一个播放网络视频例子.可以自行试试.今天分析的都是下几篇介绍各个模块 ...

  2. MVP设计模式的实现

    MVP:界面与业务逻辑分离在Winform中的应用 MVP,Model-View-Presenter的缩写. 在MSDN上,下载了一个示例,http://www.microsoft.com/china ...

  3. 【xml】转义字符 <等符号出现的原因

    来源:http://www.cnblogs.com/hyd309/p/3549076.html HTML中的转义字符  HTML中<, >,&等有特别含义,(前两个字符用于链接签, ...

  4. 【python】sql语句插入中内容同时包含单引号和双引号的解决办法

    在python中调用MySQLdb模块插入数据信息,假设待输入信息data为: Hello'World"! 其中同时包含了单引号和双引号 一般插入语句为 sql = "insert ...

  5. SQLServer视图

    视图简介:通过定义 SELECT 语句以检索将在视图中显示的数据来创建视图.SELECT 语句引用的数据表称为视图的基表.在SQL Server 2005系统中,可以把视图分为3种类型,即标准视图,索 ...

  6. INNODB

    INNODB,是Mysql5.7的默认存储引擎,是事务安全的,支持ACID,具有提交,回滚和crash-recovery[灾备]能力,以保护用户数据. 优势:一旦Server崩溃,Innodb会自动保 ...

  7. Android之WebView学习

    WebView常用方法 WebSettings 在使用WebView前我们都要进行相关的配置,常见的操作如下: WebSettings settings = mWebView.getSettings( ...

  8. Page Object Model (Selenium, Python)

    时间 2015-06-15 00:11:56  Qxf2 blog 原文  http://qxf2.com/blog/page-object-model-selenium-python/ 主题 Sel ...

  9. service(启动方式)

  10. 同一内网不能网段ping 不通

    [root@NB sysconfig]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use ...