Python的多处理



我是python的鸟,我想翻译一些文件,所以我写了一个python shell。当我这样做时,有些不对劲。它不起作用,我打印结果。我认为代码有效但翻译失败是错误的。我不知道它是什么不起作用。

<multiprocessing.pool.ApplyResult object at 0x102ee3990>
<multiprocessing.pool.ApplyResult object at 0x102ee3a10>
<multiprocessing.pool.ApplyResult object at 0x102ee3ad0>

#!/usr/bin/env python
#coding=utf-8
from multiprocessing import Pool
from googletrans import Translator
import os
import sys
import re    
import urllib,urllib2
from time import time
reload(sys)
sys.setdefaultencoding( "utf-8" ) 

def Gtranslate(pregs):
    filepath='./test/'
    pathDir =  os.listdir(filepath)
    for allDir in pathDir:
        filename = os.path.join('%s%s' % (filepath, allDir))
        content = open(filename,'r').read()
        for preg in pregs: 
            rex=re.search(preg,content)
            if rex:
                translator = Translator()
                res=translator.translate(rex.group(1), dest='zh-CN').text
                content = re.sub(preg,rex.group().replace(rex.group(1),res),content)
                print res
        with open(filename,'w+') as f:
            f.write(content)

def main():
    pool = Pool(processes=4)
    partterns = ("script_names*(s*englishs*:s*"([sS]*?)"s*)s*;","script_set_attributes*(s*attributes*:s*"s*solutions*"s*,s*values*:s*"([sS]*?)"s*)s*;","script_set_attributes*(s*attributes*:s*"s*descriptions*"s*,s*values*:s*"([sS]*?)"s*)s*;")  
    for par in partterns:
        result = pool.apply_async(Gtranslate, (par, ))
        print result
        print par
    pool.close()
    pool.join()
    if result.successful():
        print 'successful'

if __name__ == "__main__":
    t1=time()
    main()
    print "cost time is: {:.2f}s".format(time() - t1)

要从AsyncResult中获取结果,请调用 AsyncResult.get

for par in partterns:
    result = pool.apply_async(Gtranslate, (par, ))
    print par, result.get()

但是,这将防止并行执行。

而是收集异步结果,然后在提交到子进程的所有模式后调用AsyncResult.get

async_results = []
for par in patterns:
    result = pool.apply_async(Gtranslate, (par,))
    async_results.append((par, result))
for par, result in async_results:
    print par
    print result.get()

相关内容

  • 没有找到相关文章

最新更新