About multithreaded operations.
For IO operations, such as accessing a Web site, writing to a disk that takes time to wait for a response, multiple CPUs can hardly improve efficiency.
For CPU-intensive operations, such as this format conversion, can be done concurrently with multiple CPUs.
But for Python, Python has a lock on the Gil global interpreter, causing only one Python thread to be accepted by the interpreter. Therefore, Python can only use thread operations on IO operations.
1 #Coding:utf82 ImportCSV3 fromXml.etree.ElementTreeImportElement,elementtree4 ImportRequests5 fromStringioImportStringio6 fromTest_retractxmlImportPretty7 8 defDownload (URL):9 #The IO operation is slow because data cannot be obtained directly. As this step: is to send the request, wait for the data, in the process of waiting to give up the CPU, their own sleep. TenResponse = Requests.get (url,timeout=3) One ifResponse.ok: A returnStringio (response.content) - - defCsvtoxml (scsv,fxml): the #This is CPU intensive, and multiple CPUs can operate simultaneously -Reader =Csv.reader (scsv) -headers =Reader.next () -headers = Map (LambdaH:h.replace (' ',"'), headers) + -Root = Element ('Data') + forRowinchReader: AErow = Element ('Row') at root.append (Erow) - forTag,textinchZip (headers,row): -E =Element (TAG) -E.text =text - Erow.append (e) - in Pretty (Root) -ET =ElementTree (Root) to et.write (fxml) + - the defhandle (SID): * Print 'Download ... (%d)'%Sid $URL ='Http://table.finance.yahoo.com/table.csv?s=%s.sz'Panax NotoginsengURL%= str (SID). Rjust (6,'0') -RF =Download (URL) the ifRf isNone:return + A Print 'Convert to XML ... (%d)'%Sid thefname = str (SID). Rjust (6,'0')+'. XML' +With open (fname,'WB') as WF: - csvtoxml (RF, WF) $ $ fromThreadingImportThread - - " " the t = Thread (target=handle,args= (1,) ) - T.start ()Wuyi the print ' main thread ' - " " Wu classMyThread (Thread): - def __init__(SELF,SID): AboutThread.__init__(self) $Self.sid =Sid - - defRun (self): - handle (SELF.SID) A +Threads = [] the forIinchXrange (1,11): -t =MyThread (i) $ threads.append (t) the T.start () the the forTinchThreads: the T.join () - in Print 'Main thread' the #T.join () #阻塞函数 to ensure that the main thread exits after all child threads have ended the About the " " the #这是串行的方法 the For SID in Xrange (1,11): + print ' Download ... (%d) '% SID - url = ' Http://table.finance.yahoo.com/table.csv?s=%s.sz ' the URL%= str (SID). Rjust (6, ' 0 ')Bayi rf = Download (URL) the if RF is None:continue the - print ' Convert to XML ... (%d) '% SID - fname = str (SID). Rjust (6, ' 0 ') + '. Xml ' the with open (fname, ' WB ') as WF: the csvtoxml (RF, WF) the " "
PYTHON168 's study notes 7