如何限制Spacy使用的CPU数量?

时间:2018-05-25 21:36:09

标签: spacy

如何限制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()

1 个答案:

答案 0 :(得分:4)

我对自己问题的回答是,"调用操作系统并使用名为taskset的Linux实用程序。"

PaymentApproval pa;
/* populate regular fields in pa */

List<PaymentProposalDetail> details = new List<PaymentProposalDetail>();

foreach (data UIdata in UIdataStruct)
{
    PaymentProposalDetail pd = new PaymentProposalDetail();
    pd.InvoiceNumber = UIdata.invoiceNumber;
    pd.PayAmount = UIdata.payAmount;
    pd.InvoiceCurrenct = UIdata.invoiceCurrency;

    details.Add(pd);
}

pa.Details = details.ToArray(); 

此特定解决方案将运行进程限制为核心#1和#2。这个解决方案对我来说已经足够了。