Python是支持多线程的,主要通过thread和threading两个模块来实现,其中thread模块比较底层(或者说是轻量级的),而threading模块对thread做了一些包装,可以更加方便地被使用。

Note1:The thread module has been renamed to _thread in Python 3.The 2to3 tool will automatically adapt imports when converting yoursources to Python 3; however, you should consider using the high-levelthreading module instead.

Note2:Starting with Python 2.6,  threading module provides PEP 8 compliant aliases andproperties to replace the camelCase names that were inspired by Java’sthreading API. This updated API is compatible with that of themultiprocessing module. However, no schedule has been set for thedeprecation of the camelCase names and they remain fully supported inboth Python 2.x and 3.x.

1、thread模块

thread.LockType :锁对象的一种,用于线程同步

thread.error:线程异常

thread.start_new_thread(function,args[,kwargs]):创建一个新的线程

function:线程执行函数

args:线程执行函数的参数,是一个元组类型数据

kwargs:字典(可为空)

return:线程的表示符

thread.exit() :线程退出函数

thread.allocate_lock() :生成一个未锁状态的锁对象返回值

2、threading模块

经常和Queue结合使用,Queue模块中提供了同步的、线程安全的队列类,包括FIFO(先入先出)队列Queue,LIFO(后入先出)队列LifoQueue,和优先级队列PriorityQueue。这些队列都实现了锁原语,能够在多线程中直接使用。可以使用队列来实现线程间的同步。

常用函数:threading.active_count()  :返回当前线程对象Thread的个数(Return the number ofThreadobjects currently alive.  The returned count is equal to the length of the list returned byenumerate().)

threading.enumerate()  :返回当前运行的线程对象Thread(包括后台的)的list

threading.Condition()  :返回条件变量对象的工厂函数,主要用户线程的并发(A factoryfunction that returns a new condition variable object. A conditionvariableallows one or more threads to wait until they are notified by another thread.)

threading.current_thread()  :返回当前的线程对象Thread(Return the current Threadobject, corresponding to the caller’s thread of control.  If the caller’sthread of control was not created through the threading module, a dummy threadobject with limited functionality is returned.)

threading.Lock()  :返回一个新的锁对象,是在thread模块的基础上实现的,与acquire()和release()结合使用(A factory function that returns a new primitive lock object.Once a thread hasacquired it, subsequent attempts to acquire it block, untilit is released; anythread may release it.)

常用类:threading.Thread  :一个表示线程控制的类, 这个类常被继承(A class that represents athread of control.  This class can be safelysubclassed in a limited fashion.)

thraeding.Timer  :定时器,线程在一定时间后执行(A thread that executes a functionafter a specified interval has passed.)

threading.ThreadError  :引发中各种线程相关异常(Raised for various threading-relatederrors as described below.  Note thatmany interfaces use RuntimeError insteadof ThreadError.)

Thread对象:This class represents an activity that is run in a separate thread of control. There are two ways to specify the activity:

by passing a callable object to the constructor

by overriding the run() method in a subclass.

No other methods (except for the constructor) should be overridden in a subclass.  In other words,  only  override the __init__() and run() methods of this class.

Once a thread object is created, its activity must be started by calling the thread’s start() method.  This invokes the run() method in aseparate thread of control.

Once the thread’s activity is started, the thread is considered ‘alive’. Itstops being alive when its run() method terminates – either normally, orby raising an unhandled exception.  The is_alive() method tests whether thethread is alive.

threading.Thread(group = None, target = None, name = None, args = () kwars = {})

group : 应该为None(reserved for future extension when aThreadGroup class is implemented.)

target : 可以传入一个函数用于run()方法调用

name : 线程名 默认使用"Thread-N"

args : 元组, 表示传入target函数的参数

kwargs : 字典, 传入target函数中关键字参数作者:Andrew_liu

链接:http://www.jianshu.com/p/86b8e78c418a

來源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

属性:

name  #线程表示, 没有任何语义

doemon  #布尔值, 如果是守护线程为True, 不是为False, 主线程不是守护线程, 默认threading.Thread.damon = False

类方法:

run()  #用以表示线程活动的方法。

start()  #启动线程活动。

join([time])  #等待至线程中止。这阻塞调用线程直至线程的join() 方法被调用中止-正常退出

或者抛出未处理的异常-或者是可选的超时发生。

isAlive(): 返回线程是否活动的。

getName(): 返回线程名。

setName(): 设置线程名。

参考资料:

转载本文请联系原作者获取授权,同时请注明本文来自郗强科学网博客。

链接地址:http://blog.sciencenet.cn/blog-3360373-1089604.html

上一篇:Win7下Gephi9.2的安装

下一篇:window7下anaconda安装

python多线程并发数量控制简书_科学网—python多线程编程 - 郗强的博文相关推荐

  1. python中rand函数是什么意思_科学网-Python中*和**作用及随机数产生函数 np.random.rand()/numpy.random.unifo...-张伟的博文...

    [第一部分] Python中的 * 和 ** ,能够让函数支持任意数量的参数,它们在函数定义和调用中,有着不同的目的! 一. 打包参数 * 的作用:在函数定义中,收集所有位置参数到一个新的元组,并将整 ...

  2. python中format函数用法简书_从Python安装到语法基础,这才是初学者都能懂的爬虫教程...

    Python和PyCharm的安装:学会Python和PyCharm的安装方法 变量和字符串:学会使用变量和字符串的基本用法 函数与控制语句:学会Python循环.判断语句.循环语句和函数的使用 Py ...

  3. python可视化水平双向箭头_科学网—Python matplotlib quiver—画箭头、风场、量场图 - 张伟的博文...

    用像素点坐标画图 箭头关键的一个参数是长度,长度可以通过参数scale来设置,如果你多次使用quiver(),只要保证参数scale一致,那么箭头长度就会与风速 的值成正比,可按照下面我贴出的代码那样 ...

  4. python 面板数据分析_科学网—Python中的结构化数据分析利器-Pandas简介 - 郑俊娟的博文...

    此文转载于XXXXXX处... Pandas是python的一个数据分析包,最初由AQR Capital Management于2008年4月开发,并于2009年底开源出来,目前由专注于Python数 ...

  5. python频次统计图_科学网—Python小例:统计文本中单词出现的频次 - 康建的博文...

    import re zen=''' The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better ...

  6. python社会网络分析教授_科学网—python 社会网络分析工具之networkx - 郗强的博文...

    1.networkx 2.igraph 3.SNAP 1.networkx NetworkX是一个用Python语言开发的图论与复杂网络建模工具,内置了常用的图与复杂网络分析算法,可以方便的进行复杂网 ...

  7. python中几种读取文件的方法_科学网—python中几类文件的读写 - 郗强的博文

    所使用python模块为json.csv等. 一.json文件读写 1.JSON简介:其全名为JavaScript Object Notation是一种轻量级的数据交换格式.Json最广泛的应用是作为 ...

  8. python 网络_科学网-python 社会网络分析工具之networkx-郗强的博文

    1.networkx 2.igraph 3.SNAP 1.networkx NetworkX是一个用Python语言开发的图论与复杂网络建模工具,内置了常用的图与复杂网络分析算法,可以方便的进行复杂网 ...

  9. python对数运算符号_科学网—Python中算数运算符之注意及np.logspace - 张伟的博文...

    (一)算数运算符 数字2 是一个整数的例子. 长整数 不过是大一些的整数. 3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里,52.3E-4表示52.3 * 10-4. (-5+4j ...

最新文章

  1. Java项目:前台+后台精品水果商城系统设计和实现(java+Springboot+ssm+mysql+jsp+maven)
  2. 竞态条件的赋值_《Java并发编程实战》读书笔记一:基础知识
  3. Sql注入式攻击和一些防范技巧
  4. 设计模式学习笔记(1)之单例模式
  5. netty socket_Netty:另一种Web(Socket)服务器
  6. 笨方法python_笨方法学习Python(11-20)
  7. Java类加载器 以及类加载器的委托模型
  8. python源码中的学习笔记_第7章_字符串
  9. MySQL数据库管理命令和远程管理问题
  10. linux安装网易云
  11. 中兴F450电信光猫改桥接模式
  12. 中文女和程序员的爱情奇遇
  13. 新浪开发平台实现分享
  14. SAP系统 - ABAVN固定资产报废解析之部分报废案例
  15. 有源代码就可以自行开发通达信交易接口了吗?
  16. 新手程序员如何提升自己的编程能力
  17. Android实现多线程下载并显示通知
  18. CMake make makefile
  19. html中字体行间距怎么写,css行间距(html文字行间距怎么设置)
  20. oracle语句块begin的好处,sql – 如何从Oracle中的declare / begin / end块返回行?

热门文章

  1. 回文素数(Prime Palindrome)
  2. Ant Design Pro of Vue——蚂蚁金服中后台系统框架搭建
  3. struts2_action获取web资源的两种方式
  4. javaweb/ssm/jsp/mysql婚恋交友网站开发与设计
  5. matlab 伽马分布函数,伽马分布(Γ分布的分布函数)
  6. BurpSuite 破解版(含注册机,无后门)下载注册详细教程
  7. 自学还是辞职大数据_说到数据库,为什么“宝贝,我无法辞职”
  8. 使用SM2签名加密算法在jar包中使用时报错
  9. python中a_谈a++,a--,--a,++a,a+=1,a-=1,在python中是如何的。
  10. 3.通过Opencv读取和保存图片