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. linux centos yum安装LAMP环境

    centos 6.5 1.yum安装和源代码编译在使用的时候没啥区别,但是安装的过程就大相径庭了,yum只需要3个命令就可以完成,源代码需要13个包,还得加压编译,步骤很麻烦,而且当做有时候会出错,源 ...

  2. SSIS之-DTS对象&事件

    1.Dts 是类 Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel 类的一个实例,Dts 对象有 7 个属性和一个方法,以下是DTS ...

  3. 有关Fragment的知识点

    关于判断Fragment是否可见,可以尝试参考使用Fragment中的两个方法: final boolean isHidden() Return true if the fragment has be ...

  4. Ajax回调函数返回的中文字符串乱码问题

    通过ajax提交请求,返回的response所带的中文字符串一直显示为乱码,写了如下代码也无效: response.setCharacterEncoding("UTF-8"); r ...

  5. linux 挂载光盘:mount: you must specify the filesystem type

    尝试挂载光盘镜像时出现mount: you must specify the filesystem type 使用-t auto -t iso9660 或不加参数都搞不定,最后在以下链接找到解决办法: ...

  6. MySQL exists的用法介绍

    有一个查询如下: 1 SELECT c.CustomerId, CompanyName   2 FROM Customers c   3 WHERE EXISTS(   4     SELECT Or ...

  7. Jfreechart初案例--饼图

    1.action @Controller(value = "pieAction") @Scope("prototype") public class PieAc ...

  8. 滑动式折叠菜单 - Slashdot's Menu

    折叠菜单让你在尽可能小的地方放置尽可能多的内容,同时加大了操作的简便性,因此,深受前台设计师的喜爱.随着大家对动画效果的钟爱,折叠菜单也开始“动”起来了,本文介绍的就是 DimX 制作的滑动式折叠菜单 ...

  9. js中setTimeout()时间参数设置为0的探讨

    起因源于一道前端笔试题: var fuc = [1,2,3]; for(var i in fuc){ setTimeout(function(){console.log(fuc[i])},0); co ...

  10. Linux系统值得一看的学习方法及路线图

    网络是一个很神奇的东西,现代人的生活离不开网络,网络已深入人们的工作,生活,娱乐等方方面面.网络之所以无处不在,是因为它提供了诸多的网络服务,所以网络服务是网络的灵魂. 互联网上的各种网络服务是架构在 ...