Re: BUG #14583: plpythonu : subprocess not working in function
Kieran McCusker <kieran.mccusker@gmail.com>
From: Kieran McCusker <kieran.mccusker@gmail.com>
To: paulo.nuno.leite@gmail.com
Cc: pgsql-bugs@postgresql.org
Date: 2017-03-09T18:14:49Z
Lists: pgsql-bugs
On 8 March 2017 at 17:51, <paulo.nuno.leite@gmail.com> wrote:
> The following bug has been logged on the website:
>
> Bug reference: 14583
> Logged by: Paulo B
> Email address: paulo.nuno.leite@gmail.com
> PostgreSQL version: 9.5.6
> Operating system: Windows
> Description:
>
> I have the following function that runs a command:
>
>
> CREATE OR REPLACE FUNCTION public.classify_test()
> RETURNS void AS
> $BODY$
> import os
> from subprocess import Popen, PIPE
> import subprocess
> import os
> from subprocess import Popen, PIPE
>
> echo=Popen(('echo', '123'), shell=True)
>
> p = subprocess.Popen(['python.exe', '-u','test.py'], stdin = echo.stdout,
> universal_newlines = True, shell = True)
>
> $BODY$
> LANGUAGE plpythonu VOLATILE
> COST 100;
> ALTER FUNCTION public.classify_test()
> OWNER TO postgres;
>
> if I run this fuction as select classify_test() it will not run the python
> script. However, if I run it in the command line it will work. It works as
> well in SublimeText2 IDE. I have been trying to find what the bug or
> problem
> is, but had no success. I am using python2.7, I have psycopg2 installed.
>
> Why doesn't postgresql execute the python script?
>
>
>
>
>
> --
> Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-bugs
Hi
I had the same problem migrating to python3. My python3 code now looks like
$BODY$
import subprocess
plpy.execute('drop server if exists fi_server_{} cascade'.format(id))
try:
subprocess.check_output(['/usr/local/bin/ogr_fdw_info',
'-s', 'fi_server_{}'.format(id),
'-t', 'import.t{0}_{1}'.format(id, worksheet),
'-d', filename,
'-w', str(worksheet)
])
except subprocess.CalledProcessError, e:
if e.returncode == 1:
plpy.execute(e.output)
return None
else:
return e.output
Hope this helps
Kieran