如何使用python多线程从XMl插入数据库?

时间:2011-11-30 10:32:24

标签: python xml multithreading

有人可以告诉我python多线程的可能方法是什么? 我有一个xml文件(163 MB)。我的任务是

  1. 读取xml文件
  2. 将数据插入数据库(许多表格)
  3. 记录日志文件中插入行的数量
  4. 我已经有python代码读取执行上述1,2和3步骤的xml文件。实际上,我想使用多线程来加速这个过程。我不知道如何开始工作。

    这是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

2 个答案:

答案 0 :(得分:1)

我已经查看了您的代码。我不认为多线程是你问题的答案。

  • 并非所有xml库都相同,lxmllibxml2的python接口,用C语言编写,是我用过的最快的。
  • 如果您还没有,请考虑哪些操作在时间上比较昂贵。与内存访问相比,文件操作很昂贵。每次调用数据库都很昂贵。从互联网下载内容非常昂贵。
  • 我不知道你正在使用什么数据库和数据库接口,但你应该真的使用内置参数化而不是你的消毒功能。

我建议您重新构建代码以使用批处理方法:

  • 处理整个xml文件,将您需要的数据提取到python数据结构中。
  • 不要在文件系统中使用单独的文件作为处理或缓存的一部分。尽量避免将某些内容写入您希望稍后作为同一作业的一部分阅读的文件。
  • 预先缓存您的表格查找,例如创建一个select name,id from table字典,而不是100 select id from table where name=%s的调用。
  • 确定一次性创建的外键表条目,并一次创建它们,更新id/name缓存。
  • 将数据库更新分组为executeMany次来电。
  • 如果您需要整理不再用作外键的表中的行,请使用单个SQL命令在最后执行此操作。

答案 1 :(得分:0)

好吧,根据我的理解,您不能拆分XML阅读,但您可以做的,可能取决于您的XML结构和数据库结构并行插入数据库。遗憾的是,在没有看到XML和数据库结构的情况下,也不知道数据库的约束(例如,保持xml记录与auto_increment id的顺序) - 很难为您提供一些在您的特定情况下适合您的解决方案。