Python中thread 多线程处理
2015-06-04 17:29
363 查看
参考文章:
Python模块学习 —- thread 多线程处理 http://python.jobbole.com/81544/
Python 标准库提供了 thread 和 threading 两个模块来对多线程进行支持。其中, thread 模块以低级、原始的方式来处理和控制线程,而 threading 模块通过对 thread 进行二次封装,提供了更方便的 api 来处理线程。 虽然使用 thread 没有 threading 来的方便。
下面是一段简单的代码,在我们自己的机器上运行一下
第一次运行的结果是:19878
第二次运行的结果是:32257
并没有得到我们想到的结果
显然,程序每次运行的结果都是不一样的,那么这是为什么呢?其实就是程序运行的过程中发生了线程的抢夺导致的。
解决上面问题的一个有效的方法就是加锁,
琐可以保证在任何时刻,最多只有一个线程可以访问共享资源。
下面是经过修改时候的程序:
程序运行的结果是:100000
显然这次程序运行才是正确的。
下面解释一下程序中用到的几个函数:
thread.start_new_thread ( function , args [ , kwargs ] )
函数将创建一个新的线程,并返回该线程的标识符(标识符为整数)。参数 function 表示线程创建之后,立即执行的函数,参数 args 是该函数的参数,它是一个元组类型;第二个参数 kwargs 是可选的,它为函数提供了命名参数字典。函数执行完毕之后,线程将自动退出。如果函数在执行过程中遇到未处理的异常,该线程将退出,但不会影响其他线程的执行。 下面是一个简单的例子:
thread.exit()
结束当前线程。调用该函数会触发 SystemExit 异常,如果没有处理该异常,线程将结束。
thread.get_ident ()
返回当前线程的标识符,标识符是一个非零整数。
thread.interrupt_main ()
在主线程中触发 KeyboardInterrupt 异常。子线程可以使用该方法来中断主线程。下面的例子演示了在子线程中调用 interrupt_main ,在主线程中捕获异常:
lock.acquire ( [ waitflag ] )
获取琐。函数返回一个布尔值,如果获取成功,返回 True ,否则返回 False 。参数 waitflag 的默认值是一个非零整数,表示如果琐已经被其他线程占用,那么当前线程将一直等待,只到其他线程释放,然后获取访琐。如果将参数 waitflag 置为 0 ,那么当前线程会尝试获取琐,不管琐是否被其他线程占用,当前线程都不会等待。
lock.release ()
释放所占用的琐。
lock.locked ()
判断琐是否被占用。
Python模块学习 —- thread 多线程处理 http://python.jobbole.com/81544/
Python 标准库提供了 thread 和 threading 两个模块来对多线程进行支持。其中, thread 模块以低级、原始的方式来处理和控制线程,而 threading 模块通过对 thread 进行二次封装,提供了更方便的 api 来处理线程。 虽然使用 thread 没有 threading 来的方便。
下面是一段简单的代码,在我们自己的机器上运行一下
__author__ = 'CMZ' #coding:utf-8 import time import thread import random count = 0 def threadTest(): global count for i in xrange(10000): count+=1 for i in range(10): thread.start_new_thread(threadTest,()) time.sleep(3) print count
第一次运行的结果是:19878
第二次运行的结果是:32257
并没有得到我们想到的结果
显然,程序每次运行的结果都是不一样的,那么这是为什么呢?其实就是程序运行的过程中发生了线程的抢夺导致的。
解决上面问题的一个有效的方法就是加锁,
琐可以保证在任何时刻,最多只有一个线程可以访问共享资源。
下面是经过修改时候的程序:
__author__ = 'CMZ' #coding:utf-8 import time import thread import random count = 0 lock = thread.allocate_lock() #创建一个锁对象 def threadTest(): global count ,lock lock.acquire()#获取锁 for i in xrange(10000): count+=1 lock.release() #释放锁 for i in range(10): thread.start_new_thread(threadTest,()) time.sleep(3) print count
程序运行的结果是:100000
显然这次程序运行才是正确的。
下面解释一下程序中用到的几个函数:
thread.start_new_thread ( function , args [ , kwargs ] )
函数将创建一个新的线程,并返回该线程的标识符(标识符为整数)。参数 function 表示线程创建之后,立即执行的函数,参数 args 是该函数的参数,它是一个元组类型;第二个参数 kwargs 是可选的,它为函数提供了命名参数字典。函数执行完毕之后,线程将自动退出。如果函数在执行过程中遇到未处理的异常,该线程将退出,但不会影响其他线程的执行。 下面是一个简单的例子:
#coding=utf-8 import thread, time def threadFunc(a = None, b = None, c = None, d = None): print time.strftime('%H:%M:%S', time.localtime()), a time.sleep(1) print time.strftime('%H:%M:%S', time.localtime()), b time.sleep(1) print time.strftime('%H:%M:%S', time.localtime()), c time.sleep(1) print time.strftime('%H:%M:%S', time.localtime()), d time.sleep(1) print time.strftime('%H:%M:%S', time.localtime()), 'over' thread.start_new_thread(threadFunc, (3, 4, 5, 6)) #创建线程,并执行threadFunc函数。 time.sleep(5) ''' 运行结果 #17:26:18 3 #17:26:19 4 #17:26:20 5 #17:26:21 6 #17:26:22 over '''
thread.exit()
结束当前线程。调用该函数会触发 SystemExit 异常,如果没有处理该异常,线程将结束。
thread.get_ident ()
返回当前线程的标识符,标识符是一个非零整数。
thread.interrupt_main ()
在主线程中触发 KeyboardInterrupt 异常。子线程可以使用该方法来中断主线程。下面的例子演示了在子线程中调用 interrupt_main ,在主线程中捕获异常:
__author__ = 'CMZ' #coding:utf-8 import thread,time thread.start_new_thread(lambda :(thread.interrupt_main(),),()) try: time.sleep(2) except KeyboardInterrupt,e: print 'error:',e print 'over'
lock.acquire ( [ waitflag ] )
获取琐。函数返回一个布尔值,如果获取成功,返回 True ,否则返回 False 。参数 waitflag 的默认值是一个非零整数,表示如果琐已经被其他线程占用,那么当前线程将一直等待,只到其他线程释放,然后获取访琐。如果将参数 waitflag 置为 0 ,那么当前线程会尝试获取琐,不管琐是否被其他线程占用,当前线程都不会等待。
lock.release ()
释放所占用的琐。
lock.locked ()
判断琐是否被占用。
相关文章推荐
- 安装python MySQLdb报错:pymemcompat.h:10:20: 致命错误的解决
- python中引用与复制用法实例分析
- windows环境,用python实现SSH操作
- <PY><Numpy><Scipy>调整音量
- Python命令tab键自动补全
- Python进程间通信用法实例
- python中__slots__用法实例
- python目录操作
- python模块ConfigParser操作配置文件
- python协程用法实例分析
- robot framework用python扩展编写自定义library
- Python获取系统默认字符编码的方法
- <PY><NumPy><SciPy>图像翻转切割遮罩
- Python Knowledge
- python中__call__内置函数用法实例
- python判断用户注册中用户名是否包含非法字符
- python抽象基类用法实例分析
- python基于multiprocessing的多进程创建方法
- 用python画个花朵
- python 列表转换成字符串