Re: persistant psql feature suggestion
От | PeterKorman |
---|---|
Тема | Re: persistant psql feature suggestion |
Дата | |
Msg-id | 20030629210814.GA15100@eigenvision.com обсуждение исходный текст |
Ответ на | Re: persistant psql feature suggestion ("Andrew Dunstan" <andrew@dunslane.net>) |
Список | pgsql-hackers |
On Sun, Jun 29, 2003 at 02:15:26PM -0400, Andrew Dunstan wrote: > We don't need to read and write on the same fd. > > The real right way is to detect when the psql client exits - possible when > the perl program spawns it, like shown below. > > As always, TIMTOWTDI > > andrew > > -------------------------- > > use strict; > use IO::Handle; > use POSIX ":sys_wait_h"; > > my $curpos; > my $fifofile = shift || usage(); > my $database = shift || usage(); > > open(FILE,$fifofile) || die $!; > my $psqlpid = open(OUTPIPE,"|-"); > unless (defined($psqlpid)) { die $!; } > if ($psqlpid == 0) > { > exec("psql -a $database") || die $!; > } > > # must be parent here > sub REAPER > { > my $waitedpid; > while (($waitedpid = waitpid(-1,WNOHANG)) > 0) > { > if ($waitedpid == $psqlpid) { exit 0; } > } > $SIG{CHLD} = \&REAPER; # loathe sysV > } > > $SIG{CHLD} = \&REAPER; > > OUTPIPE->autoflush(); > > for (;;) > { > for ($curpos = tell(FILE); $_ = <FILE>; $curpos = tell(FILE)) > { > print OUTPIPE $_; > } > sleep(1); > seek(FILE, $curpos, 0); > } > > > sub usage > { > print STDERR "usage: ",$0," fifofile database\n"; > exit 1; > } I dropped into autopilot without considering James Pye's original phrase: "The ability for psql to remain in the background reading from stdin and writing to stdout until explicitly killed" I went straight to thinking about handling the psql output which matched /ERROR/ differently from other output. I like your solution. But I think I'd code it: my $psqlpid = open(OUTPIPE,"|-") || die qq(cant fork: $!); Sometimes I'm too stupid to remember that that perl gives you the fork (the knife and the kitchen sink) even if you dont realize you asked for it;-) Cheers, JPK
В списке pgsql-hackers по дате отправления: