python opencc 卡住

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.

改为communicate()就解决了。

修改后的代码为

   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')


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值