많은 문자열 정규 표현식을 하나의 긴 문자열과 매치 할 때마다 구분 기호를 매치하려고합니다.여러 개의 정규 표현식 문자열이 파이썬과 일치합니다.
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
사용 동시에 한번에 많은 정규식을 검색한다. 더 많은 코어가 사용되지 않는 이유는 무엇입니까? 또한, 모든 파이썬 라이브러리는 이렇게 빨리 할 수 있습니까?
에 대한
from multiprocessing.dummy import Pool
교체는 아마 관련 : https://stackoverflow.com/questions/26432411/multiprocessing-dummy-in-python-is-not-utilising -100-cpu/26432431 –
이 문서는 GIL이 실제로 동시에 처리하는 두 개의 스레드 (http://lucasb.eyer.be/snips/python-thread-pool.html)를 방지하기 때문에 스레드 풀이 IO 바운드 작업에만 유용함을 제안합니다. 아마도 다중 프로세스 풀을 사용하여 구조 조정할 수있는 방법이 있을까요? – Phil
'sch '의 구조는 무엇입니까? 그것은'alice | bob | charlie | ... '또는 좀 더 복잡한 무언가와 같은 정적 문자열의 변화가있는 하나의 긴 정규식입니까? 더 복잡한 경우에는 역 추적이 필요할 수 있습니까? – tripleee