threading 模块包含方法
- threading.currentThread(): 返回当前的线程变量。
- threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
- threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。
除了使用方法外,线程模块同样提供了Thread类来处理线程,Thread类提供了以下方法:
- run(): 用以表示线程活动的方法。
- start():启动线程活动。
- join([time]): 等待至线程中止。这阻塞调用线程直至线程的join() 方法被调用中止-正常退出或者抛出未处理的异常-或者是可选的超时发生。
- isAlive(): 返回线程是否活动的。
- getName(): 返回线程名。
- setName(): 设置线程名。
通过实例化threading.Thread类创建线程
import time
import threading
def test_thread(para='hi', sleep=3):
"""线程运行函数"""
time.sleep(sleep)
print(para)
def main():
# 创建线程
thread_hi = threading.Thread(target=test_thread)
thread_hello = threading.Thread(target=test_thread, args=('hello', 1))
# 启动线程
thread_hi.start()
thread_hello.start()
print('Main thread has ended!')
if __name__ == '__main__':
main()
Threading模块
import sys
import threading
import time
class myThread(threading.Thread):
def __init__(self, output_list):
threading.Thread.__init__(self)
self.output_list = output_list
def run(self):
for i in self.output_list:
print (str(i)+' ')
#time.sleep(0.1)
def runFunc():
thread1 = myThread([i for i in range(1,250)])
thread2 = myThread([i for i in range(250,500)])
thread3 = myThread([i for i in range(500,750)])
thread4 = myThread([i for i in range(750,1000)])
#thread5 = myThread([i for i in range(1,1000)])
thread1.start()
thread2.start()
thread3.start()
thread4.start()
#thread5.start()
thread1.join()
thread2.join()
thread4.join()
thread3.join()
#thread5.join()
sys.stdout.flush()
runFunc()
互斥锁
Queue 模块中的常用方法:
import threading,sys,time
count =0
class myThread(threading.Thread):
def run(self):
global count
time.sleep(0.1)
threadLock.acquire() #获得锁
count += 1 #临界区代码
threadLock.release() #释放锁
threadLock = threading.Lock() #建立锁对象
for i in range(500):
thread = myThread()
thread.start()
for i in range(500):
thread.join()
print(count)
import queue, threading, requests
hosts = ['http://www.baidu.com','http://taobao.com','http://amazon.com',
'http://ibm.com','http://apple.com']
queue = queue.Queue()
class ThreadUrl(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
#从任务队列中取出一个URL
host = self.queue.get()
#爬取网页内容
url = requests.get(host)
print (url.text[1:24])
#发出一项任务已完成的信号
self.queue.task_done()
#建立爬虫线程
for i in range(3):
t = ThreadUrl(queue)
t.setDaemon(True) #开启守护线程
t.start()
#将需要爬取的URL加入队列
for host in hosts:
queue.put(host)
#等待所有线程任务完成
queue.join()
print ("Done")
Queue模块:队列同步
- Queue.qsize() 返回队列的大小
- Queue.empty() 如果队列为空,返回True,反之False
- Queue.full() 如果队列满了,返回True,反之False
- Queue.full 与 maxsize 大小对应
- Queue.get([block[, timeout]])获取队列,timeout等待时间
- Queue.get_nowait() 相当Queue.get(False)
- Queue.put(item) 写入队列,timeout等待时间
- Queue.put_nowait(item) 相当Queue.put(item, False)
- Queue.task_done() 在完成一项工作之后,Queue.task_done()函数向任务已经完成的队列发送一个信号
- Queue.join() 实际上意味着等到队列为空,再执行别的操作
import queue
import threading
import time
exitFlag = 0
class myThread (threading.Thread):
def __init__(self, threadID, name, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print ("开启线程:" + self.name)
process_data(self.name, self.q)
print ("退出线程:" + self.name)
def process_data(threadName, q):
while not exitFlag:
queueLock.acquire()
if not workQueue.empty():
data = q.get()
queueLock.release()
print ("%s processing %s" % (threadName, data))
else:
queueLock.release()
time.sleep(1)
threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = ["One", "Two", "Three", "Four", "Five"]
queueLock = threading.Lock()
workQueue = queue.Queue(10) #创建队列
threads = []
threadID = 1
# 创建新线程
for tName in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1
# 填充队列
queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()
# 等待队列清空
while not workQueue.empty():
pass
# 通知线程是时候退出
exitFlag = 1
# 等待所有线程完成
for t in threads:
t.join()
print ("退出主线程")