开发者

how to use stanford parser with threads

hello I want to use stanford parser wuth threads but I dont know how to 开发者_运维知识库do that with thread pool. I want that all threads will do this:

LexicalizedParser.apply(Object in)

but I dont want to create all the time new object of LexicalizedParser because it will load lp = new LexicalizedParser("englishPCFG.ser.gz"); and it will take 2 sec for each obj. what can I do?

thanks!


Guess it's too late but a thread safe version is there: http://nlp.stanford.edu/software/lex-parser.shtml


You can use ThreadLocal. It allows you to keep one instance of parser per thread. Thus any created instance of parser will never be used from more than one thread.

Usually it shouldn't create more instances than CPUs*cores you have.

For me it is ~4-5 instances (if I disable Hyper Threading on my quadcore).


P.S. Not related to StanfordNLP. Sometimes poor class implementations contain static fields and modify them in non-thread safe way. General safe parallelization approach for such implementations would be:

  1. move computation part into separate process;
  2. launch (CPUs*cores) number of processes with computations.
  3. use IPC technic for communicating between main/background processes.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜