Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

332
Visualizações
Why do Inline::Python functions that print fail to be redirected?

Here is the simplest reproducible for my problem:

#!/usr/bin/env perl
use 5.16.3;
say "Got back ", test_print();
use Inline Python => <<'END_OF_PYTHON_CODE';
def test_print() -> int:
    print("In the python test print")
    return 32
END_OF_PYTHON_CODE

When run simply:

$ perl small.pl
In the python test print
Got back 32

But when redirected:

$ perl small.pl | tee foo
Got back 32
$ cat foo
Got back 32

What could I be doing wrong such that Inline::Pythoncode fails to print to a redirected output?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Py_Finalize() isn't called to properly destruct the Python interpreter.

Thankfully, the module exposes this function as py_finalize, allowing us to call it ourselves. Add the following to your program:

END { Inline::Python::py_finalize(); }

Demo:

use feature qw( say );

use Inline Python => <<'END_OF_PYTHON_CODE';

def test_print() -> int:
    print("In the python test print")
    return 32

END_OF_PYTHON_CODE

END { Inline::Python::py_finalize() if $ARGV[0]; }

say "Got back ", test_print();
$ perl a.pl 0
In the python test print
Got back 32

$ perl a.pl 0 | cat
Got back 32

$ perl a.pl 1
In the python test print
Got back 32

$ perl a.pl 1 | cat
In the python test print
Got back 32
over 4 years ago · Santiago Trujillo Relatório

0

The complete thing to do is:

BEGIN {
    # Unbuffer Python's output
    $ENV{PYTHONUNBUFFERED}=1;

    # Unbuffer Perl's output
    select((select(STDOUT), $|=1)[0]);
    select((select(STDERR), $|=1)[0]);
}
...
END { 
    # Shut down the Python interpreter.
    Inline::Python::py_finalize();
}

You must also unbuffer Perl's output. Thanks to ikegami who commented to that effect in a now-deleted post, and reminded me about py_finalize().

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda