2017-12-01 4 views
0

많은 문자열 정규 표현식을 하나의 긴 문자열과 매치 할 때마다 구분 기호를 매치하려고합니다.여러 개의 정규 표현식 문자열이 파이썬과 일치합니다.

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 코어가 이용되고 : multiprocessing 사용 동시에 한번에 많은 정규식을 검색한다. 더 많은 코어가 사용되지 않는 이유는 무엇입니까? 또한, 모든 파이썬 라이브러리는 이렇게 빨리 할 수 ​​있습니까?

+1

에 대한

from multiprocessing.dummy import Pool

교체는 아마 관련 : https://stackoverflow.com/questions/26432411/multiprocessing-dummy-in-python-is-not-utilising -100-cpu/26432431 –

+0

이 문서는 GIL이 실제로 동시에 처리하는 두 개의 스레드 (http://lucasb.eyer.be/snips/python-thread-pool.html)를 방지하기 때문에 스레드 풀이 IO 바운드 작업에만 유용함을 제안합니다. 아마도 다중 프로세스 풀을 사용하여 구조 조정할 수있는 방법이 있을까요? – Phil

+0

'sch '의 구조는 무엇입니까? 그것은'alice | bob | charlie | ... '또는 좀 더 복잡한 무언가와 같은 정적 문자열의 변화가있는 하나의 긴 정규식입니까? 더 복잡한 경우에는 역 추적이 필요할 수 있습니까? – tripleee

답변

0

multiprocessing.dummyPool 클래스는 멀티 프로세싱 대신 스레딩을 사용합니다. 이것은 글로벌 인터프리터 잠금이 문제라는 것을 의미합니다. 실제 다중 처리를 사용하고자합니다. 그것을 위해,

from multiprocessing import Pool

+0

이제 Windows 7, python 3 (anaconda 4.4)에서 10 분 이상 계속 실행되고 강제 중지해야합니다. – bob

+0

ipython 콘솔에서 실행하면 문제가 발생했습니다. 명령 프롬프트에서 잘 작동합니다. – bob