基于Windows的python2.7多处理

2024-10-01 02:35:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉多处理,但我需要解析大量的xml文件,解析是一个更大的应用程序的一部分。解析可以在“后台”中进行,因此它不会影响主应用程序。为此,我有以下几点(练习)

from lxml import etree
from StringIO import StringIO
import multiprocessing as mp


def parseBookXML(xmlFile):

    f = open(xmlFile)
    xml = f.read()
    f.close()

    tree = etree.parse(StringIO(xml))

    #now write it out to file
    with open("out_filename", "w") as fout:
    fout.write(ET.tostring(tree))

def do_process_file():
     process = mp.Process(target=parseBookXML, args=(c:\my_xml_file.xml))
     process.start()

实际上,我想调用parseBookXML函数,将一个文件作为参数传递给它(甚至是一个xml文件的列表),并让解析在它自己的过程中完成。在

正常情况下(即进程启动())效果很好。在

^{pr2}$

上面的工作很好,xml_文件被解析没有错误。在

但是使用do_process_file函数会给我带来错误,大致如下所示

AttributeError no module etree

我在Windows上使用python2.7,而不是使用REPL或交互式解释器。我读过windows和linux不同

我读过一点关于多重处理不在窗口酸洗(我不知道这意味着什么)

我真正需要知道的是如何使这种函数起作用。在


Tags: 文件函数fromimport应用程序defasmp