Python Threading
threading总结:http://www.cnblogs.com/tkqasn/p/5700281.html
Threading模块下的thread类的使用:
| 函数 | 描述 |
|---|---|
| run() | 通常需要重写,编写代码实现 做需要的功能。定义线程功能的函数 |
| getName() | 获得线程对象名称 |
| setName() | 设置线程对象名称 |
| start() | 启动线程 |
| join(timeout) | 在一个线程B中调用threada.join(),则threada结束后,线程B才会接着threada.join()往后运行,timeout可以设置等待时间。 |
| setDaemon() | 主线程A启动了子线程B,调用b.setDaemaon(True),则主线程结束时,会把子线程B也杀死,必须在start()之前调用。默认为False。 |
| isDaemon() | 判断线程是否随主线程一起结束。 |
| isAlive() | 检查线程是否在运行中。 |
| Lock() | 分配一个LockType类型的锁对象 |
| Rlock() | 递归锁,支持多把锁 |
| acquire() | 加锁,锁住相应的资源 |
| release() | 释放锁,释放相应的资源 |
锁:lock,RLock,BoundedSemaphore
1. 普通锁 threading.Lock() 只能解一把锁,有多把锁就会出不来,把自己锁死
import threading
import time
lock = threading.Lock()
def download(url):
lock.acquire()
time.sleep(1)
print(url)
lock.release()
urls = [123, 234, 456, 567, 678]
for i in urls:
t = threading.Thread(target=download, args=[i])
t.start()
print("end")
2. 递归锁 threading.Rlock() 可以解多把锁
import threading import time lock = threading.RLock() def download(url): lock.acquire() lock.acquire() time.sleep(1) print(url) lock.release() lock.release() urls = [123, 234, 456, 567, 678] for i in urls: t = threading.Thread(target=download, args=[i]) t.start() print("end")
3.信号量:threading.BoundedSemaphore
举例:厕所外面加锁,一个厕所有2个坑。 这就是所谓的信号量
import threading
import time
lock = threading.BoundedSemaphore(2)
def download(url):
lock.acquire()
time.sleep(1)
print(url)
lock.release()
urls = [123, 234, 456, 567, 678]
for i in urls:
t = threading.Thread(target=download, args=[i])
t.start()
print("end")
Condition, Event
1.Event全部锁住等待释放
python线程的Event用于主线程控制其他线程的执行,事件主要提供了三个方法 set、wait、clear。
事件处理的机制:全局定义了一个“Flag”,如果“Flag”值为 False,那么当程序执行 event.wait 方法时就会阻塞,如果“Flag”值为True,那么event.wait 方法时便不再阻塞。
- clear:将“Flag”设置为False
- set:将“Flag”设置为True
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import threading
def do(event):
print 'start'
event.wait()
print 'execute'
event_obj = threading.Event()
for i in range(10):
t = threading.Thread(target=do, args=(event_obj,))
t.start()
event_obj.clear()
inp = raw_input('input:')
if inp == 'true':
event_obj.set()
2.Condition想锁几个就锁几个,使得线程等待,只有满足某条件时,才释放n个线程
import threading
def run(n):
con.acquire()
con.wait()
print("run the thread: %s" % n)
con.release()
if __name__ == '__main__':
con = threading.Condition()
for i in range(10):
t = threading.Thread(target=run, args=(i,))
t.start()
while True:
inp = input('>>>')
if inp == 'q':
break
con.acquire()
con.notify(int(inp))
con.release()
线程池
from concurrent.futures import ThreadPoolExecutor
import time from concurrent.futures import ThreadPoolExecutor def download(url): time.sleep(1) print("result:", url) pool = ThreadPoolExecutor(2) urls = [ "www.baidu.com", "www.sina.com", "www.163.com", ] for url in urls: print("开始请求", url) pool.submit(download, url) print("end")
join实例
import threading
import time
def sayhi(num):
time.sleep(num)
print("running on number:%s" % num)
if __name__ == '__main__':
thread_list = []
for i in range(10):
t1 = threading.Thread(target=sayhi, args=(i,))
t1.start()
thread_list.append(t1)
for n in thread_list:
n.join()
print(threading.active_count())
print("-------------主线程------------")

浙公网安备 33010602011771号