如何加速二进制调用?

2024-03-28 12:14:20 发布

您现在位置:Python中文网/ 问答频道 /正文

实际上我使用的是一个字典标记器,它只作为二进制文件分发。它获取一个字符串作为输入,然后打印结果。我在python中发现了一个使用Popen的包装器,它工作得很好,但是每个调用需要1000毫秒才能完成(与输入无关)。我想加快它,因为我需要调用这个过程很多次。你知道吗

一个好方法是在python程序的整个执行过程中保持进程的活动性,但我不知道如何做到这一点。你知道吗

下面是包装代码的一部分

_input = sentences #Actually a string with "\n" escapes

p = Popen([self._cmd_path],
                shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)

(stdout, stderr) = p.communicate(_input)

# Check the return code.
if p.returncode != 0:
    print stderr
    raise OSError('Command Failed')

_output = stdout.decode(encoding)

#...other elaborations with the output

这是我称之为的脚本:

#!/bin/sh

# Set paths

BIN=xxx/bin
CMD=xxx/cmd
LIB=xxx/lib

OPTIONS="-opt1 -opt2 -opt3"

EXAMPLER1=${CMD}/file1.perl
EXAMPLER2=${BIN}/binary-file
EXAMPLER3=${LIB}/file2
EXAMPLER4=${LIB}/file3

$EXAMPLER1 -i -a $EXAMPLER4 $* |
$EXAMPLER2 $OPTIONS $EXAMPLER3

Tags: thecmdinputoutputbin过程libwith