Python通过两个标准库thread和threading提供对线程的支持。
thread提供了低级别的、原始的线程以及一个简单的锁。
threading基于Java的线程模型设计。
锁(Lock)和条件变量(Condition)在Java中是对象的基本行为(每一个对象都自带了锁和条件变量),而在Python中则是独立的对象。
start_new_thread()要求一定要有前两个参数。所以,就算我们想要运行的函数不要参数,我们也要传一个空的元组。 test_thread.py
#! /usr/bin/env python
# -*- coding:utf-8 -*-
import thread
import time
from time import sleep,ctime test_list = [5,8]
def f1():
print 'start f1 at:',ctime()
sleep(5)
print 'f1 done at:',ctime()
def f2():
print 'start f1 at:',ctime()
sleep(3)
print 'f1 done at:',ctime()
def main():
print "start:",ctime()
thread.start_new_thread(f1,())
thread.start_new_thread(f2,())
sleep(6)
print "all end:",ctime()
if __name__ == '__main__':
main() test_threading.py
#! /usr/bin/env python
# -*- coding:utf-8 -*-
import threading
import time exitFlag = 0 class myThread(threading.Thread):
def __init__(self,threadID,name,delay):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.delay = delay
def run(self):
print "Starting" + self.name
print_time(self.name,self.delay,5)
print "Exiting" + self.name
def print_time(threadName,delay,counter):
while counter:
try:
if exitFlag:
thread.exit()
time.sleep(delay)
print "%s:%s" % (threadName, time.ctime(time.time()))
counter -= 1
except Exception,e:
logging.info(e)
thread1 = myThread(1,"Thread-1",1)
thread2 = myThread(2,"Thread-2",2) thread1.start()
thread2.start() print "Exiting Main Thread" python多线程threading.Lock锁的用法 #创建锁
mutex = threading.Lock()
#锁定
mutex.acquire([timeout])
#释放
mutex.release() test_threading_lock.py
#!/usr/bin/env python
# -*- coding:utf-8 -*- import threading
import time class my_thread(threading.Thread):
def run(self):
global num
time.sleep(1)
if mutex.acquire(1):
num = num + 1
msg =self.name + 'set num to '+str(num)
print msg
mutex.release()
num = 0
mutex = threading.Lock()
def test():
for i in range(5):
t = my_thread()
t.start()
if __name__ == '__main__':
test()
python 多线程中常用到的几个方法,链接地址:http://blog.chinaunix.net/uid-27571599-id-3484048.html
 

python进阶笔记 thread 和 threading模块学习的更多相关文章

  1. python学习笔记之使用threading模块实现多线程(转)

    综述 Python这门解释性语言也有专门的线程模型,Python虚拟机使用GIL(Global Interpreter Lock,全局解释器锁)来互斥线程对共享资源的访问,但暂时无法利用多处理器的优势 ...

  2. Python2.7 threading模块学习

    主要学习一下python的多线程编程,使用threading模块,threading 包括:Thread.conditions.event.rlock.semaphore等类. Thread对象可以实 ...

  3. Python机器学习笔记:sklearn库的学习

    网上有很多关于sklearn的学习教程,大部分都是简单的讲清楚某一方面,其实最好的教程就是官方文档. 官方文档地址:https://scikit-learn.org/stable/ (可是官方文档非常 ...

  4. Python基础笔记系列十:模块

    本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 模块 #1.类比于java中的jar包,模块能让你能够有逻辑地组织你的Py ...

  5. Python之网路编程利用threading模块开线程

    一多线程的概念介绍 threading模块介绍 threading模块和multiprocessing模块在使用层面,有很大的相似性. 二.开启多线程的两种方式 1 1.创建线程的开销比创建进程的开销 ...

  6. Python 全栈开发六 常用模块学习

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve configparser hashlib 一. ...

  7. python标准库介绍——31 threading 模块详解

    threading 模块 (可选) ``threading`` 模块为线程提供了一个高级接口, 如 [Example 3-1 #eg-3-1] 所示. 它源自 Java 的线程实现. 和低级的 ``t ...

  8. python进阶(3):模块和包

    之前两天我们介绍了一些比较常用的模块,而我也说过会讲解什么是模块,今天我们就来分析分析模块和包,模块我们现阶段使用还可以而包的话现阶段我们基本很少会用到包,学的不是很清楚也没关系这些东西都是用的多了也 ...

  9. Python成长之路(常用模块学习)

    Python 拥有很多很强大的模块 主要写一下常用的几个吧 大概就是这些内容了 模块介绍 time &datetime模块 random os sys shutil json & pi ...

随机推荐

  1. centos7装机和初步运维

    1.装机-制作U盘启动盘 CentOS7是一个比较新的版本.在服务器领域用得比较多.因为前安装软件可能没有Ubuntu那样方便,所以桌面领域好像不是很多. https://www.centos.org ...

  2. JavaWeb基础: Cookie

    Cookie相关类: javax.servlet.http.Cookie 用于创建Cookie response 里通过addCookie方法添加Cookie到响应头 request里通过getCoo ...

  3. JAVA04类与对象之课后问题解决

    1.验证ClassAndObjectTest.java(使用自定义类) public class ClassAndObjectTest { public static void main(String ...

  4. Thinkphp 3.2.2 利用phpexcel完成excel导出功能

    首先百度搜索phpexcel  包,放到项目的这个目录下 接下来  是controller里的导出代码 /**导出预定产品用户信息 * 大白驴 675835721 *2016-12-12 **/pub ...

  5. go语言环境搭建

    1.官网https://golang.org/dl/下载go语言的执行器 2.此处以Windows操作系统上安装go为例,安装go1.7.3.windows-amd64.msi 3.配置go环境变量, ...

  6. MongoDB aggregate 运用篇 个人总结

    最近一直在用mongodb,有时候会需要用到统计,在网上查了一些资料,最适合用的就是用aggregate,以下介绍一下自己运用的心得.. 别人写过的我就不过多描述了,大家一搜能搜索到N多一样的,我写一 ...

  7. 三、Python 变量、运算符、表达式

    3.1 变量 变量是计算机内存中的一块区域,变量可以存储规定范围内的值,值可以改变,其实是将值在内存中保存地址位交给变量,变量去内存中获取,重新赋值,改变的就是内存地址位. 命名: 变量名由字母.数字 ...

  8. ssh base 写法

    BaseDao package wl.oa.dao.base; public interface BaseDao<T>{ public void saveEntry(T t); } Bas ...

  9. Spring Shedule Task之注解实现 (两次启动Schedule Task 的解决方案)

    在spring 中的新引入的task 命名空间.可以部分取代 quartz 功能,配置和API更加简单,并且支持注解方式. 第一步: 在Spring的相关配置文件中(applicationContex ...

  10. 《Pro Express.js》学习笔记——Express服务启动常规七步

    Express服务启动常规七步 1.       引用模块 var express=require('express'), compression=require('compression'), bo ...