postgresql - Python subprocess hangs with psql command -
i running following piece of python code (runs command in shell , grabs output or reports error)
import sys import subprocess def check_output(args, communicate=none, quiet=false, **kwargs): stream in ["stdout", "stderr"]: kwargs.setdefault(stream, subprocess.pipe) proc = subprocess.popen(args, **kwargs) try: out, err = proc.communicate() finally: f in (proc.stdout, proc.stderr): if f not none: f.close() proc.wait() if kwargs["stderr"] != subprocess.pipe: err = "" if proc.returncode != 0: raise exception(args, proc.returncode, err) else: if not quiet: sys.stderr.write(err) sys.stderr.flush() return out
with following arguments:
env = dict( pghost='{pg_host}', pgport='{pg_port}', pgdatabase='{pg_dbname}', pguser='{pg_user}', pgpassword='{pg_password}', ) cmd = ['psql', '-c', "insert {ft_geom} select * {ft_geom_in};"].format(**tables) check_output(cmd, shell=true, env=env)
here env
contains pg[host|user|database|port|..]
environment variables , tables
contains names of 2 tables. when run code, hangs indefinitely on proc = subprocess.popen
call. using python 2.6.5
on ubuntu 10.04.3 lts
i check no tables locked following:
select a.datname, c.relname, l.transactionid, l.mode, l.granted, a.usename, a.current_query, a.query_start, age(now(), a.query_start) "age", a.procpid pg_stat_activity join pg_locks l on l.pid = a.procpid join pg_class c on c.oid = l.relation order a.query_start;
and shows locks granted. not sure else at. need shell=true
because commands more complex, requiring bash pipes. know should ideally pass stdout.pipe
of 1 command other, not possible change @ moment.
running same command bash directly works expected, also, running without shell=true
works
the problem didn't filter newline characters in tokens passed check_output
, shell hang waiting more input. make sure escape tokens when pass them shell.
Comments
Post a Comment