Skip to content

Commit

Permalink
enable timeout option of KNP and Juman (fixes #47)
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu-g committed May 11, 2021
1 parent 0db7f98 commit 73b095b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
5 changes: 3 additions & 2 deletions pyknp/juman/juman.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ def __init__(self,
self.ignorepattern = ignorepattern
self.pattern = pattern
if server is not None:
self.analyzer = Analyzer(backend='socket', server=server, port=port, socket_option='RUN -e2\n')
self.analyzer = Analyzer(backend='socket', timeout=timeout, server=server, port=port,
socket_option='RUN -e2\n')
else:
cmds = [self.command] + self.options
if self.rcfile:
cmds += ['-r', self.rcfile]
self.analyzer = Analyzer(backend='subprocess', multithreading=multithreading, command=cmds)
self.analyzer = Analyzer(backend='subprocess', multithreading=multithreading, timeout=timeout, command=cmds)

if self.rcfile and not os.path.isfile(os.path.expanduser(self.rcfile)):
raise Exception("Can't read rcfile (%s)!" % self.rcfile)
Expand Down
5 changes: 3 additions & 2 deletions pyknp/knp/knp.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ def __init__(self,
self.rcfile = rcfile
self.pattern = pattern
if server is not None:
self.analyzer = Analyzer(backend='socket', server=server, port=port, socket_option='RUN -tab -normal\n')
self.analyzer = Analyzer(backend='socket', timeout=timeout, server=server, port=port,
socket_option='RUN -tab -normal\n')
else:
cmds = [self.command] + self.options
if self.rcfile:
cmds += ['-r', self.rcfile]
self.analyzer = Analyzer(backend='subprocess', multithreading=multithreading, command=cmds)
self.analyzer = Analyzer(backend='subprocess', multithreading=multithreading, timeout=timeout, command=cmds)
self.jumanpp = jumanpp

if self.rcfile and not os.path.isfile(os.path.expanduser(self.rcfile)):
Expand Down
15 changes: 12 additions & 3 deletions pyknp/utils/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ class Analyzer(object):
command (list): サブプロセスに渡すコマンド
"""

def __init__(self, backend, multithreading=False, server=None, port=None, socket_option=None, command=None):
def __init__(self,
backend,
multithreading=False,
server=None,
port=None,
socket_option=None,
command=None,
timeout=180,
):
self.backend = backend
self.multithreading = multithreading
self.timeout = timeout

self.socket = None
self.server = server
Expand All @@ -30,9 +39,9 @@ def query(self, input_str, pattern):
self.socket = Socket(self.server, self.port, self.socket_option)
else:
if self.multithreading is True:
self.subprocess = SubprocessThreadSafe(self.command)
self.subprocess = SubprocessThreadSafe(self.command, timeout=self.timeout)
else:
self.subprocess = Subprocess(self.command)
self.subprocess = Subprocess(self.command, timeout=self.timeout)

if self.socket:
return self.socket.query(input_str, pattern=pattern)
Expand Down

0 comments on commit 73b095b

Please sign in to comment.