python进阶:第七章(并发编程核心技术)

问题一:如何使用多线程?

问题内容:我们通过雅虎网站获取中国股市某支股票csv数据文件,现在要现在多只股票的csv数据,并将其转换为xml文件。
如何使用线程来提高下载并处理的效率?

解决方案:使用标准库threading.Thread 创建线程,在每个线程中下载并转换一支股票数据。

详细内容看代码:

import  csv
from xml.etree.ElementTree import ElementTree,Element
import requests
from io import StringIO
from threading import Thread

def pretty(e,level=0):
    if len(e) > 0:
        e.text = '\n' + '\t' * (level + 1)
        for child in e :
            pretty(child,level + 1)
        child.tail = child.tail[:-1]
    e.tail = '\n' + '\t' * level


def download(url):
    response = requests.get(url)
    if response.ok:
        return StringIO(response.text)
#download是一个IO操作,在发送请求之后,需要等待一段时间才会有结果
#在等待时间会让出CPU,当数据来的时候才会获得CPU的执行权

def csvToXml(scsv,fxml):
    reader = csv.reader(scsv)
    values = [row for row in reader]
    headers = values[0]
    headers = list(map(lambda h:h.replace(' ',''),headers))

    #values = [value for value in values[1:] if value[0] >= '2017-01-01' ]

    root = Element('Data')
    for row in values[1:]:
        if row[0] > '2017-01-01':
            eRow = Element('Row')
            root.append(eRow)
            for tag,text in zip(headers,row):
                e = Element(tag)
                e.text = text
                eRow.append(e)

    pretty(root)
    et = ElementTree(root)
    et.write(fxml)


#使用线程进行下载转换
def handle(sid):
    print('Download ... (%d)' % sid)
    url = 'http://table.finance.yahoo.com/table.csv?s=%s.sz'
    url %= str(sid).rjust(6, '0')
    rf = download(url)
    if rf is None: return

    print('Convert to XML...(%d)' % sid)
    fname = str(sid).rjust(6, '0') + '.xml'
    with open(fname, 'wb') as wf:
        csvToXml(rf, wf)

"""
通过线程对象开启线程
t = Thread(target=handle,args=(1,))
t.start()
"""


class MyThread(Thread):
    def __init__(self,sid):
        Thread.__init__(self)
        self.sid = sid
    def run(self):
        handle(self.sid)

#同时创建十个线程
threads = []
for i in range(1,11):
    t = MyThread(i)
    threads.append(t)
    t.start()


#主线程等待子线程退出之后退出
#t.join()

#等待所有子线程结束之后主线程再退出
for t in threads:
    t.join()

print("main Thread")


# if __name__ == '__main__':
#     # #下载单独的一只股票
#     # url = 'http://table.finance.yahoo.com/table.csv?s=000001.sz'
#     # rf = download(url)
#     # if rf:
#     #     with open('000001.xml','wb') as wf:
#     #         csvToXml(rf,wf)
#
#
#     #串行下载多只股票
#     for sid in range(1,11):
#         url ='http://table.finance.yahoo.com/table.csv?s=%s.sz'
#         url %= str(sid).rjust(6,'0')
#         rf = download(url)
#         if rf is None: continue
#
#         print('Convert to XML...(%d)' % sid)
#         fname = str(sid).rjust(6,'0')+'.xml'
#         with open(fname,'wb') as wf:
#             csvToXml(rf,wf)

你可能感兴趣的:(python进阶:第七章(并发编程核心技术))