python 上的pip库里的opencc 版本非常老了,无法正常安装。于是直接去网上https://pypi.python.org/pypi/opencc-python/下载下来手动安装。
结果,装换文本一大,就会卡住。
是因为其对管道的处理没有遵循官方的说明。当数据比管道的缓冲区大时就会卡住
Popen.
wait
(
)
¶
Wait for child process to terminate. Set and return returncode
attribute.
Warning
This will deadlock when using stdout=PIPE
and/or stderr=PIPE
and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use communicate()
to avoid that.
修改后的代码为
def convert(self, text):
"""Convert text
"""
proc = subprocess.Popen([self.opencc_path, '-c', self.confg],
cwd=self.data_path,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
result,err=proc.communicate(text.encode('utf8'))
if proc.returncode:
raise RuntimeError('Failed to call opencc with exit code %s' % code)
return result.decode('utf8')