摘要:
Python 对并行化支持的名声就不是很好,如果你用过 Python 自带的线程库 thread 和 threading,你应该知道 Python 线程其实并不好用。例如:没有自带的获取线程返回值的方法,需要自己重写自己的threading。
目录:
- Python 多线程 基础
- Python 多线程 阻塞
- Python 多线程 获取返回值
- Python 多线程 数据对比测试
正文:
一. Python 多线程 基础
1.1 thread 模块
# -*- coding: UTF-8 -*-
import thread
import time
# 为线程定义一个函数
def print_time( threadName, delay):
count = 0
while count < 5:
time.sleep(delay)
count += 1
print "%s: %s" % ( threadName, time.ctime(time.time()) )
# 创建两个线程
try:
thread.start_new_thread( print_time, ("Thread-1", 2, ) )
thread.start_new_thread( print_time, ("Thread-2", 4, ) )
except:
print "Error: unable to start thread"
while 1:
pass
执行结果:
Thread-1: Thu Jan 22 15:42:17 2009
Thread-1: Thu Jan 22 15:42:19 2009
Thread-2: Thu Jan 22 15:42:19 2009
Thread-1: Thu Jan 22 15:42:21 2009
Thread-2: Thu Jan 22 15:42:23 2009
Thread-1: Thu Jan 22 15:42:23 2009
Thread-1: Thu Jan 22 15:42:25 2009
Thread-2: Thu Jan 22 15:42:27 2009
Thread-2: Thu Jan 22 15:42:31 2009
Thread-2: Thu Jan 22 15:42:35 2009
1.2 threading模块
# -*- coding: UTF-8 -*-
import threading
def func(a):
print "input is :" + a
threads = []
for i in range(10):
threads.append(threading.Thread(target=func, args=str(i)))
for i in threads:
i.start()
执行结果:
D:\Python\python.exe C:/Users/timen.xu/Desktop/CsdnSpider/Test.py
input is :0
input is :1
input is :2
input is :3
input is :4
input is :5
input is :6
input is :7
input is :8
input is :9
Process finished with exit code 0
二. Python 多线程 阻塞
2.1 阻塞的例子
import threading
import time
def foo():
time.sleep(100)
threads = []
for i in range(5):
threads.append(threading.Thread(target=foo))
for t in threads:
t.start()
你会发现你的 python 脚本不能使用 ctrl+c 来取消了,那么问题来了,为什么会出现之前说的子线程“阻塞”主线程的问题呢?其实这并不是阻塞,默认情况不加任何参数的话:主线程完成后退出,而子线程并不会退出(daemon 属性为 False),此时主线程已经完成退出,没有线程接收 ctrl+c 的信号,子线程相当于没人托管,所以不能停止子线程的输出。
2.2 解决方法
import threading
import time
def foo():
time.sleep(100)
threads = []
for i in range(5):
threads.append(threading.Thread(target=foo)
for t in threads:
t.daemon = True
t.start()
while threading.activeCount() > 0:
time.sleep(1)
首先配置 daemon 为 True,随后每一秒检查一次活跃的子线程数目,直至所有子线程结束。功能相当于手动实现了一个不会 block 信号的对所有线程调用的 join 方法。
三. Python 多线程 获取返回值
3.1 简介
其实这才是 Python 线程使用中最常遇到的问题,起了很多个线程调用了很多函数,然而直接用 threading 库基本没有办法获取返回值。
3.2 采用队列
这是目前最主流的获取线程数据的方法。使用 Queue 库创建队列实例,用来储存和传递线程间的数据。Python 的队列是线程安全的,也就是说多个线程同时访问一个队列也不会有冲突。Python 队列有三种 FIFO 先进先出,FILO 先进后出(类似栈),优先级队列(由单独的优先级参数决定顺序)。使用队列可以实现简单 生产者 – 消费者 模型
# -*- coding:utf-8 -*-
import threading
import Queue
import random
import time
class Producer(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.data = queue
def run(self):
# 生成随机数字并放入队列
for i in range(10):
num = random.randint(1,20)
print "original num", num
self.data.put(num)
time.sleep(1)
# 主线程
def main():
queue = Queue.Queue()
producer = Producer(queue)
producer.daemon = True
producer.start()
while threading.activeCount() > 0:
time.sleep(1)
if __name__ == '__main__':
main()
四. Python 多线程 数据对比测试
4.1 数据对比需求
对有鱼股票、新浪财经、富途牛牛中个股的今开、昨收、最高、最低、成交额、成交量、最新价进行数值对比,判断有鱼股票提供的数据的正确性,对比的结果误差不查过1%
需要对比的股票:hs_code_list.xls
4.2 代码编写
4.2.1 Python xlrd 获取excel需要对比数据的股票
# -*- coding:utf-8 -*-
import xlrd
# 从excel中读取数据
def read_text(excel_name, excel_table):
data_excel = []
xls = xlrd.open_workbook(excel_name)
table = xls.sheet_by_name(excel_table)
num_row = table.nrows
for i in range(num_row):
if i is not 0:
stock_market = table.row(i)[1].value
stock_code = table.row(i)[2].value
data = (stock_market, stock_code)
data_excel.append(data)
return data_excel
if __name__ == '__main__':
print len(read_text("hs_code_list.xls", "Sheet1"))
4.2.2 Python Request获取有鱼股票、新浪财经、富途牛牛个股数据
# -*- coding:utf-8 -*-
import requests
import time
import json
# 有鱼
def request_yff(market_code, stock_code, graph_tab_index, k_not_refresh, stock_type):
url = "https://market-qa.ruifusoft.com/app/v1/quote/user/query/stockdetail