多线程在python中的使用 thread
来源:互联网 发布:心理学入门 知乎 编辑:程序博客网 时间:2024/05/01 08:29
最近想学习研究一下python中使用多线程,来提高python在爬虫项目中的效率。
现在我们在网页上查询到在python中使用的多线程的使用大多数都是使用的threading模块,但是python中还有一个模块叫做的thread模块,也能够完成多线程的任务。相比较两者来说,threading是更高级别的应用模块,但thread的存在必然有存在的理由。本篇主要讲介绍一下thread在python中的应用。
资源来自python开发文档的总结:https://docs.python.org/2/library/thread.html?highlight=thread#module-thread
thread模块提供了较底层的多线程处理模块(也被称作是轻量级的处理程序),提供了同步和简单锁(mutexes或binary semaphores)的功能。
在IDLE中import thread之后,通过help(thread) 可以查看thread的相关类函数及接口函数,在帮助文档中我们主要看到了一些有关thread的lock,error相关的函数说明,线程相关的函数说明。
主要函数说明: thread对象有以下几个函数
thread.interrupt_main():在主线程中引发一个KeyboardInterrupt(通常是Ctrl+C或者是delete)异常,子线程可以通过这个函数中断主线程。
thread.exit():
触发SystemExit异常,如果没有被捕获(不作出处理),将会影响此线程退出终止。
thread.allocate_lock():
返回一个新的lock对象,锁的方法下文将要介绍,这种锁起初是没有锁定的。
thread.get_ident():
返回当前线程的id号,返回值为一个非零的整形,它的值没有直接的意义。
thread.stack_size([size]):
当建立新的线程的时候 返回栈的大小。
Lock对象有以下一个函数说明:
lock.acquire([waitflag]):
当这个函数没有括号中的可选参数的时候,这个方法可以无条件地捕获这个lock。如果此锁被占用 则该方法会等待该锁被释放后将其捕获。如果这个int型的可选参数存在的话,函数的行为和这个int的值有关系:
如果是0的话,在此lock能够被无条件地捕获的前提下 lock只是被直接捕获;当waitflag为非0值时候,lock会像之前一样被无条件捕获。如果lock被成功捕获,则返回值为True,否则返回为False.
lock.release():
释放当前的lock对象;这里的lock必须是之前被捕获的,不过不要求是被同一个lock所捕获。
lock.locked():
返回当前所的状态。如果当前所被某个锁捕获,则返回值为True,否则返回为False.
另外,对于这些方法,lock对象也可以通过with声明来使用:
import thread #导入thread模块a_lock = thread.allocate_lock()with a_lock: print "a_lock is locked while this executes"
分配一个锁:allocate_lock() -> lock object (allocate() is an obsolute synonym)
操作锁的方法:
2) 线程相关的函数说明:
创建一个新线程:start_new_thread(function,args[,kwargs]) (start_new() is an obsolete synonym)
退出线程:exit()和exit_thread() (PyThread_exit_thread() is an obsolete synonym)
一 lock相关的函数使用示例(略去error):
import threaddef print_status(a_lock): if a_lock.locked(): print "locked" else: print "not locked"a_lock = thread.allocate_lock()print_status(a_lock)a_lock.acquire()print_status(a_lock)a_lock.release()print_status(a_lock)
二 thread相关的函数使用示例:
import threaddef run(n): # a back door, can not be run 4 times if n == 4: thread.exit() for i in range(n): print ithread.start_new_thread(run,(5,))
三 解决一个同步问题
试解决以下同步问题:使用两个线程交替输出“Hello”与“World”各5次,以“Hello”开始以“World”结束。
①HelloWorld问题的同步模型建立:
semaphore h = 1, w = 0 # because the semaphore w is 0,so we should acquire the lock w to let it be zero(locked) when we use python to .thread1(){ while(true) { p(h) do something; v(w) }}thread2(){ while(true) { p(w) do something; v(h) }}
②使用Python实现上述同步模型,两个解决方案如下。
方案A用main线程和另一个线程来交替打印。
方案B使用除main线程外的另两个线程交替打印“Hello”与“World”。
import threaddef world(): for i in range(5): w_lock.acquire() # i want to print world print "world" h_lock.release() # you can print hello now w_lock.release()# main threadprint "use two threads to print hello&world"h_lock = thread.allocate_lock()w_lock = thread.allocate_lock()w_lock.acquire(); # "print world" can not be started firstthread.start_new_thread(world,())for i in range(5): h_lock.acquire() print "hello" w_lock.release()# raw_input("finished")
import threaddef hello(): for i in range(5): h_ok.acquire() print "hello" w_ok.release()def world(): for i in range(5): w_ok.acquire() print "world" h_ok.release()# main threadprint "use two threads to print hello&world"h_ok = thread.allocate_lock()w_ok = thread.allocate_lock()w_ok.acquire()thread.start_new_thread(hello,())thread.start_new_thread(world,())raw_input("finished") # !!it is necessary,in case main thread exit too early
- 多线程在python中的使用 thread
- [Python]多线程入门 --thread使用
- 多线程在python中的使用 threading
- python多线程thread/threading的使用
- C#中的多线程使用 -- Thread 类
- C#中的多线程使用 -- Thread 类
- C#中的多线程使用 -- Thread 类
- Python 多线程编程,thread
- python多线程thread
- Python thread 多线程
- python多线程模块thread
- Python 多线程 Thread包
- Python 多线程 thread
- python--thread多线程总结
- Python多线程(Thread)
- java中的Thread多线程
- python多线程(2)--thread和threading模块的使用
- ThreadLocal 在多线程中的使用
- 【LeetCode】(172)Factorial Trailing Zeroes(Easy)
- 递归
- 剑指offer 不用加减乘除做加法
- 欢迎使用CSDN-markdown编辑器
- android进阶之spinner
- 多线程在python中的使用 thread
- PAT 1010 月饼
- 《花千骨》为何被批“脑残”还能创造收视神话?
- 我不知道该怎么去做
- redis cluster 集群重启关闭
- Android:对话框(12种:警告对话框&进度条对话框&日期对话框&时间对话框)
- jor(1):简表(JOR)开源的报表工具调研
- 模拟 nbut1225 NEW RDSP MODE I
- 新手必须知道的13个Xcode小技巧