2017-12-01 10 views
0

多くの文字列正規表現を1つの長い文字列にマッチさせ、すべてのマッチでデリミタを数えようとしています。同時に、一度に多くの正規表現を検索するmultiprocessingを使用して:速く、複数の正規表現の文字列がPythonと一致する

with open('many_regex', 'r') as f: 
    sch = f.readlines() 

with open('big_string', 'r') as f: 
    text = f.read() 

import re 
def search_sch(sch,text = text): 
    delim_index = [] 
    last_found = 0 
    for match in re.finditer(sch, text): 
     count_delims = len(re.findall('##', text[last_found:match.start()])) 
     if delim_index: 
      count_delims += delim_index[-1] 
     delim_index.append(count_delims) 
     last_found = match.end() 
    return delim_index 

from multiprocessing.dummy import Pool 

with Pool(8) as threadpool: 
    matches = threadpool.map(search_sch, sch[:100]) 

threadpool.mapが実行する25S程度かかり、そして単一のCPUコアが利用されます。なぜもっと多くのコアが使われていないのか?また、任意のPythonライブラリは、これを高速に行うには?

+1

たぶん関連のため

from multiprocessing.dummy import Pool

を置き換える:https://stackoverflow.com/questions/26432411/multiprocessing-dummy-in-python-is-not-utilising -100-cpu/26432431 –

+0

このドキュメントでは、GILは実際に同時に処理する2つのスレッド(つまり、http://lucasb.eyer.be/snips/python-thread-pool.html)を防ぐため、スレッドプールはIOバウンド操作にのみ役立つことを示しています。おそらく、マルチプロセスプールを使用して再構成する方法がありますか? – Phil

+0

「sch」の構造は何ですか? 'alice | bob | charlie | ...'やもっと複雑なもののような静的な文字列を交互に並べた長い正規表現ですか?もっと複雑なケースでは、バックトラッキングが必要なのでしょうか? – tripleee

答えて

0

multiprocessing.dummyからのクラスPoolクラスは、マルチプロセッシングの代わりにスレッドを使用します。これは、グローバルインタプリタロックが問題であることを意味します。あなたは実際のマルチプロセッシングを使いたいです。そのため、

from multiprocessing import Pool

+0

これは、Windows 7、python 3(anaconda 4.4)で10分以上稼働し続け、強制停止しなければなりません。 – bob

+0

これをipythonコンソールから実行することが問題でした。コマンドプロンプトから、正常に動作します。 – bob

関連する問題