简体   繁体   English

Windows上的Python 2.7多重处理

[英]Python 2.7 Multiprocessing on Windows

I am new to multiprocessing, but I need to parse a large number of xml files, the parsing is part of a larger application. 我是多处理的新手,但是我需要解析大量的xml文件,该解析是大型应用程序的一部分。 Parsing can happen in the "background" so it doesn't affect the main application. 解析可以在“后台”进行,因此不会影响主应用程序。 To that end I have the following (exerpt) 为此,我有以下内容(摘录)

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()

Essentially I want to call the parseBookXML function, pass it a file as a parameter (or even a list of xml files) and let the parsing happen it its own process and complete. 本质上,我想调用parseBookXML函数,将文件作为参数(甚至是xml文件列表)传递给它,然后让解析过程按其自身的过程完成。

When doing this normally (ie without process.start()) it works fine. 正常执行此操作(即不使用process.start())时,它可以正常工作。

def do_process_file():
     self.parseBookXML(c:\my_xml_file.xml)

The above works fine, the xml_file is parsed no errors. 上面的工作正常,解析xml_file没有错误。

But making use of the do_process_file function gives me errors, along the lines of 但是使用do_process_file函数会给我带来以下错误:

AttributeError no module etree AttributeError没有模块etree

I am on Windows using python 2.7, not using the REPL or interactive interpreter. 我在Windows上使用python 2.7,而不是使用REPL或交互式解释器。 I have read that windows differs from linux, 我读过Windows与Linux不同,

I have read a little about mulitprocessing not pickling in Windows (i'm not sure what that means) 我已经读过一些关于在Windows中不进行酸洗的多处理(我不确定那是什么意思)

what I really need to know is how to make this kind of function work. 我真正需要知道的是如何使这种功能起作用。

from lxml import etree seems like your python does not have module lxml in it. from lxml import etree似乎您的python中没有模块lxml Some module are different based on OS. 某些模块因操作系统而异。 Please check whether module lxml is available for windows or not. 请检查模块lxml是否可用于Windows。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM