2011-11-30 88 views
0

有人可以請教我什麼是可能的方式與Python多線程? 我有一個XML文件(163 MB)。我的任務是需要如何使用python多線程從XMl插入數據庫?

  1. 讀取XML文件
  2. 將數據插入到一個DB(多表)
  3. 記錄在日誌文件中

我已經有插入的行數讀取執行上述1,2和3步驟的xml文件的python代碼。其實,我想用多線程來加速這個過程。我不知道如何開始工作。

這是XML結構。

<Content id="359366"> 
    <Title>This title</Title> 
    <SortTitle>sorting</SortTitle> 
    <PublisherEntity id="2003">ABC Publishing Group</PublisherEntity> 
    <Publisher>ABC Publishing Group</Publisher> 
    <Imprint>Revell</Imprint> 
    <Language code = "en">English</Language> 
    <GeoRight> 
     <GeoCountry code = "WW" model = "Distribution">World</GeoCountry> 
     </GeoRight> 
    <Format type = "Adobe EPUB eBook"> 
     <Identifier type = "DRMID">xxx-xxx-xx</Identifier> 
     <Identifier type = "ISBN">1234567</Identifier> 
     <SRP currency = "SGD">18.89</SRP> 
     <WholesaleCost currency = "SGD">11.14</WholesaleCost> 
     <OnSaleDate>01 Sep 2010</OnSaleDate> 
     <MinimumSoftwareVersion number="1.x">Adobe Digital Editions</MinimumSoftwareVersion> 
     <DownloadFileName>HouseonMalcolmStreet9781441213877</DownloadFileName> 
     <SecurityLevel value="ACS4">Adobe Content Server 4</SecurityLevel> 
     <ContentFileSize>473923</ContentFileSize> 
     <DownloadUrl>http://xxx.xx.com/</DownloadUrl> 
     <DownloadIDType>CRID</DownloadIDType> 
     <DrmInfo> 
      <Copy> 
       <Enabled>1</Enabled> 
       <Selections>2</Selections> 
       <Interval type = "Days">7</Interval> 
      </Copy> 
      <Print> 
       <Enabled>1</Enabled> 
       <Selections>20</Selections> 
       <Interval type = "Days">7</Interval> 
      </Print> 
      <Lend> 
       <Enabled>0</Enabled> 
      </Lend> 
      <ReadAloud> 
       <Enabled>0</Enabled> 
      </ReadAloud> 
      <Expires> 
       <Enabled>0</Enabled> 
       <Interval type = "Days">-1</Interval> 
      </Expires> 
     </DrmInfo> 
     </Format> 
    <Creator rank="1" id="923710"> 
     <Name>name</Name> 
     <FileAs>Kelly, Leisha</FileAs> 
     <Role id="aut">Author</Role> 
    </Creator> 
    <SubTitle>A Novel</SubTitle> 
    <Edition></Edition> 
    <Series></Series> 
    <Coverage></Coverage> 
    <AgeGroup></AgeGroup> 
    <ContentType></ContentType> 
    <PublicationDate>09/01/2010</PublicationDate> 
    <ShortDescription>description</ShortDescription> 
    <FullDescription>full desc</FullDescription> 
    <Image type = "Cover Image">http://xxx.xx.jpg</Image> 
    <Image type = "Thumbnail Image">http://xxx.xx.jpg</Image> 
    <Subject code="FIC000000">Fiction</Subject> 
    <Subject code="FIC014000">Historical Fiction</Subject>  
</Content> 

這裏是現有的Python代碼download

+1

我建議你介紹一下你的當前代碼,並計算出不同方面所花費的時間。我認爲多線程不一定會加快任務速度。 – MattH

+0

謝謝。我發佈了xml結構和當前代碼文件 – hhkhaing

回答

0

嗯,你不能用XML來分割閱讀,但是你可以做什麼,可能取決於你的XML結構和DB結構並行插入數據庫。不幸的是,沒有看到XML和數據庫結構,也不知道數據庫的約束(例如,保持xml記錄的順序和auto_increment id的順序) - 很難就某種解決方案給出建議,以便在特定情況下適用於您。

+0

謝謝。我發佈了xml結構和當前代碼文件 – hhkhaing

1

我看過你的代碼。我不認爲多線程是解決您的問題的答案。

  • 不是所有的XML庫都是平等的,lxml是一個Python接口libxml2,這是寫在C和我用最快的。
  • 請考慮,如果您尚未確定哪些操作在時間上比較昂貴。與內存訪問相比,文件操作是昂貴的。每次調用數據庫都很昂貴。從互聯網下載東西非常昂貴。
  • 我不知道你在使用什麼數據庫和db界面,但你應該使用內置的參數化而不是你的清理功能真的

我建議你重新構建你的代碼使用批處理方式:

  • 處理整個XML文件中提取需要轉換爲Python數據結構的數據。
  • 不要在文件系統中使用單獨的文件作爲處理或緩存的一部分。儘量避免將某些內容寫入您稍後想要作爲同一作業一部分閱讀的文件中。
  • 預緩存您的表查找,例如創建一個select name,id from table的字典,而不是撥打select id from table where name=%s的100個電話。
  • 確定哪些外鍵表條目需要一次創建並一次創建,並更新id/name緩存。
  • 將數據庫更新到executeMany調用(如果可用)。
  • 如果您需要從不再用作外鍵的表中整理行,最後使用單個SQL命令來完成。
相關問題