如何限制Spacy使用的CPU数量?
我想从一大组句子中提取词性和命名实体.由于RAM的限制,我首先使用Python NLTK将我的文档解析成句子.然后我迭代我的句子并使用nlp.pipe()进行提取.然而,当我这样做时,Spacy消耗了我的整个计算机; Spacy使用每个可用的CPU.这样做并不好,因为我的电脑是共享的.如何限制Spacy使用的CPU数量?这是我迄今为止的代码:
# require
from nltk import *
import spacy
# initialize
file = './walden.txt'
nlp = spacy.load( 'en' )
# slurp up the given file
handle = open( file, 'r' )
text = handle.read()
# parse the text into sentences, and process each one
sentences = sent_tokenize( text )
for sentence in nlp.pipe( sentences, n_threads=1 ) :
# process each token
for token in sentence : print( "\t".join( [ token.text, token.lemma_, token.tag_ ] ) )
# done
quit()
Run Code Online (Sandbox Code Playgroud)
我对自己的问题的回答是,“调用操作系统并使用名为taskset 的Linux 实用程序。”
# limit ourselves is a few processors only
os.system( "taskset -pc 0-1 %d > /dev/null" % os.getpid() )
Run Code Online (Sandbox Code Playgroud)
该特定解决方案将运行进程限制在核心 #1 和 #2 上。这个解决方案对我来说已经足够好了。
| 归档时间: |
|
| 查看次数: |
593 次 |
| 最近记录: |