首页 > 代码库 > Python168的学习笔记7

Python168的学习笔记7

关于多线程操作。

对于IO操作,如访问网站,写入磁盘这种需要时间等待响应的操作,多个cpu也几乎不能提高效率。

对于CPU密集型操作,如这个格式转换,可以通过多个cpu同时去进行。

但是对于python来讲,python存在GIL全局解释器的锁,导致只有一个python线程能被解释器接收。所以等于python只能对IO操作使用线程操作。

 1 #coding:utf8
 2 import csv
 3 from xml.etree.ElementTree import Element,ElementTree
 4 import requests
 5 from StringIO import StringIO
 6 from test_retractxml import pretty
 7 
 8 def download(url):
 9     #IO操作很慢,因为不能直接得到数据。如这步:是发送请求,等待数据,在等待的过程中让出CPU,自己睡眠。
10     response = requests.get(url,timeout=3)
11     if response.ok:
12         return StringIO(response.content)
13 
14 def csvToxml(scsv,fxml):
15     #这是CPU密集型操作,多个CPU可以同时操作
16     reader = csv.reader(scsv)
17     headers = reader.next()
18     headers = map(lambda h:h.replace( ,‘‘),headers)
19     
20     root = Element(Data)
21     for row in reader:
22         eRow = Element(Row)
23         root.append(eRow)
24         for tag,text in zip(headers,row):
25             e = Element(tag)
26             e.text = text
27             eRow.append(e)
28             
29     pretty(root)
30     et = ElementTree(root)
31     et.write(fxml)
32     
33     
34 def handle(sid):
35     print Download ...(%d) % sid
36     url = http://table.finance.yahoo.com/table.csv?s=%s.sz
37     url %= str(sid).rjust(6,0)
38     rf = download(url)
39     if rf is None:return
40     
41     print Convert to XML...(%d) % sid
42     fname = str(sid).rjust(6,0)+.xml
43     with open(fname,wb) as wf:
44         csvToxml(rf, wf)
45         
46 from threading import Thread
47 
48 ‘‘‘
49 t = Thread(target=handle,args=(1,))
50 t.start()
51 
52 print ‘main thread‘
53 ‘‘‘
54 class MyThread(Thread):
55     def __init__(self,sid):
56         Thread.__init__(self)
57         self.sid = sid
58         
59     def run(self):
60         handle(self.sid)
61 
62 threads = []
63 for i in xrange(1,11):
64     t = MyThread(i)
65     threads.append(t)
66     t.start()
67     
68 for t in threads:
69     t.join()
70     
71 print main thread
72 #t.join()#阻塞函数,保证主线程在所有子线程结束后再退出
73 
74 
75 ‘‘‘
76     #这是串行的方法
77     for sid in xrange(1,11):
78         print ‘Download ...(%d)‘ % sid
79         url = ‘http://table.finance.yahoo.com/table.csv?s=%s.sz‘
80         url %= str(sid).rjust(6,‘0‘)
81         rf = download(url)
82         if rf is None:continue
83         
84         print ‘Convert to XML...(%d)‘ % sid
85         fname = str(sid).rjust(6,‘0‘)+‘.xml‘
86         with open(fname,‘wb‘) as wf:
87             csvToxml(rf, wf)
88 ‘‘‘

 

Python168的学习笔记7