[26506] in Perl-Users-Digest
Perl-Users Digest, Issue: 8663 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Nov 14 00:05:37 2005
Date: Sun, 13 Nov 2005 21:05:04 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sun, 13 Nov 2005 Volume: 10 Number: 8663
Today's topics:
Re: can you please optimize this sub? <someone@example.com>
wow shit! I ran into troubles with threads <fisherNOPASARAN@uch.net>
Re: wow shit! I ran into troubles with threads <1usa@llenroc.ude.invalid>
Re: wow shit! I ran into troubles with threads <fisherNOPASARAN@uch.net>
Re: wow shit! I ran into troubles with threads <1usa@llenroc.ude.invalid>
Re: wow shit! I ran into troubles with threads <fisherNOPASARAN@uch.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 14 Nov 2005 01:22:14 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: can you please optimize this sub?
Message-Id: <aFRdf.106190$S4.98199@edtnps84>
Serge A. Ribalchenko wrote:
>
> I feel this subroutine is looking ugly. Can you optimize/squize it?
>
> Input: number of seconds
> Output: string XXdays XXhours XXmin XXsec
>
> sub time2str($) {
> my $t=$_[0];
> my $retval="";
> if ($t >= 86400) {
> $retval= sprintf("%dd ", $t/86400);
> $t%= 86400;
> } else { $retval= "0d "; }
> if ($t >= 3600) {
> $retval.= sprintf("%02dh", $t/3600);
> $t%= 3600;
> } else { $retval.= "00h"; }
> if ($t >= 60) {
> $retval.= sprintf("%02dm", $t/60);
> $t%= 60;
> } else { $retval.= "00m"; }
> return $retval.= sprintf("%02ds",$t);
> }
sub time2str($) {
sprintf '%dd %02dh%02dm%02ds', $_[0] / 86400, ( gmtime $_[0] )[2,1,0]
}
John
--
use Perl;
program
fulfillment
------------------------------
Date: Mon, 14 Nov 2005 02:06:13 +0200
From: "Serge A. Ribalchenko" <fisherNOPASARAN@uch.net>
Subject: wow shit! I ran into troubles with threads
Message-Id: <20051114020613.08e62e6d.fisherNOPASARAN@uch.net>
--Signature=_Mon__14_Nov_2005_02_06_13_+0200_aAT0j+8BBer/cYn/
Content-Type: text/plain; charset=US-ASCII
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
Hi all,
This is not a request for help, it is JFYI.
I ran into troubles with threads module, my script just silently dies while some
threads are created, and some aren't.
After a lot of time I found that 'use threads' must be inserted before 'use strict' ;(
'perldoc perlthrtut' does not accent attention on that.
--
Best wishes,
~ Serge. pubkeys: http://uch.net/~fisher/keys.asc
~fingerprint : 4346 2766 BC96 E77F 5BFF 1E7C 12C2 3852 E5FD DC34
--Signature=_Mon__14_Nov_2005_02_06_13_+0200_aAT0j+8BBer/cYn/
Content-Type: application/pgp-signature
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFDd9T8X9BKv7kKDR0RArmsAKCIRCxqltKZ6ZwIg33c3fnJqtMNcQCeN+FT
e33IcEX/5811EMk1SJdsW5c=
=38vx
-----END PGP SIGNATURE-----
--Signature=_Mon__14_Nov_2005_02_06_13_+0200_aAT0j+8BBer/cYn/--
------------------------------
Date: Mon, 14 Nov 2005 00:18:33 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: wow shit! I ran into troubles with threads
Message-Id: <Xns970DC46EB336Dasu1cornelledu@127.0.0.1>
"Serge A. Ribalchenko" <fisherNOPASARAN@uch.net> wrote in
news:20051114020613.08e62e6d.fisherNOPASARAN@uch.net:
> Re: wow shit! I ran into troubles with threads
This is a family group. I think most of us would appreciate it if you kept
your language clean.
> This is not a request for help, it is JFYI.
> I ran into troubles with threads module, my script just silently dies
> while some threads are created, and some aren't.
> After a lot of time I found that 'use threads' must be inserted before
> 'use strict'
I seriously doubt that. Can you explain why that is necessary?
See also: http://www.perl.com/pub/a/2002/09/04/threads.html?page=1
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: Mon, 14 Nov 2005 03:26:51 +0200
From: "Serge A. Ribalchenko" <fisherNOPASARAN@uch.net>
Subject: Re: wow shit! I ran into troubles with threads
Message-Id: <20051114032651.5b1960ff.fisherNOPASARAN@uch.net>
--Signature=_Mon__14_Nov_2005_03_26_51_+0200_y8O2hxiFumJNFJbG
Content-Type: text/plain; charset=US-ASCII
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
On Mon, 14 Nov 2005 00:18:33 GMT
"A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
> > Re: wow shit! I ran into troubles with threads
>
> This is a family group. I think most of us would appreciate it if you kept
> your language clean.
Oh, I'm sorry. My language is pure literatural, without any orthographycal
mistakes, the problem is in fact that english is not _my_ language.
I did learn it by Hollywood's movies, by songs of U.S. bands, you know,
everytime I switch my TV on I hear these words are widely used, so I
decided that it's up to your culture. (or maybe absence of ?..)
Try to talk with me in my native language, and you possibly understand me.
Please excuse me.
> > This is not a request for help, it is JFYI.
> > I ran into troubles with threads module, my script just silently dies
> > while some threads are created, and some aren't.
> > After a lot of time I found that 'use threads' must be inserted before
> > 'use strict'
>
> I seriously doubt that. Can you explain why that is necessary?
That's my fault. Problem is with Thread::Queue module.
Please keep in mind that I am a newbie in perl and in programming in general.
below is my script. I had cutted non-english comments..
==============
#!/usr/bin/env perl
#
my $LogFile= "./logfile";
my $LockFile= "./lock";
my $MAXSENDERS= 3;
# -------------------------------------
use Config;
use DBI;
use threads;
use threads::shared;
use Thread::Queue;
# here
use strict;
use warnings;
$Config{useithreads} || die "Your perl interpreter hasn't compiled with ithreads support";
my $MyName=$0; $MyName=~ s/(.*\/)?(.*)/$2/;
my $ExitFlag=0;
my $dq= Thread::Queue->new;
sub logit($) {
open LOG, ">>$LogFile";
my ($s, $m, $h, $d, $M, $y)= localtime(time); $y+=1900; $M+=1;
printf LOG "%02d.%02d.%04d %02d:%02d:%02d %s[$$]: %s\n",
$d, $M, $y, $h, $m, $s, $MyName, @_;
close LOG;
}
sub SIGTERMhandler {
logit "Caught a SIG$_[0]";
$ExitFlag=1;
}
-e $LockFile && do {
print "Another incarnation of mine is running.\n"; exit 1; };
open L, ">$LockFile" || do {
print "Cannot create lock file $LockFile: $!\n"; exit 1; };
my $pid= fork();
unless (defined $pid) { print "fork() failed\n"; exit 1; }
unless ($pid eq 0) { print L "$pid\n"; close L; exit 0; }
my $dbh= DBI->connect("dbi:Pg:dbname=nibelung;host=127.0.0.1","cms","cms")
|| do { logit "DBI::connect: $DBI::errstr"; exit; };
my $sth= $dbh->prepare("SELECT * from messages where msg_status = 0")
|| do { logit "DBI::prepare: $DBI::errstr"; exit; };
my $row;
$SIG{'QUIT'} = \&SIGTERMhandler;
$SIG{'TERM'} = \&SIGTERMhandler;
$SIG{'HUP'} = 'IGNORE';
logit "Successfully started.";
sub sender() {
my $row; my $t;
$t= threads->self->tid();
logit "thread $t entering loop";
while ($row= $dq->dequeue) {
#logit "thread $t dst_addr: $$row{dst_addr}";
logit "thread $t: $row";
}
}
my @thread;
foreach (1..$MAXSENDERS) {
$thread[$_]= threads->create(\&sender);
}
logit "entering main loop";
until ($ExitFlag) {
$sth->execute || do { logit "DBI::prepare: $DBI::errstr"; exit; };
while ($row=$sth->fetchrow_hashref) {
logit "enqueue";
# how can I enqueue a hashref here?
$dq->enqueue(42);
}
sleep 1;
}
foreach (0..$MAXSENDERS) {$dq->enqueue(undef);}
my $thr;
foreach $thr (threads->list) {
if ($thr->tid && !threads::equal($thr, threads->self)) {
$thr->join;
}
}
$dbh->disconnect();
logit "Normal shutdown.";
unlink $LockFile;
exit;
=============
--
Best wishes,
~ Serge. pubkeys: http://uch.net/~fisher/keys.asc
~fingerprint : 4346 2766 BC96 E77F 5BFF 1E7C 12C2 3852 E5FD DC34
--Signature=_Mon__14_Nov_2005_03_26_51_+0200_y8O2hxiFumJNFJbG
Content-Type: application/pgp-signature
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFDd+fiX9BKv7kKDR0RAhE5AJ4kkpceuh8c09Y0rGwbZ0uXMAo04ACcC5fh
iqj0vOdwolvYqS3T/m8Izhw=
=6Eav
-----END PGP SIGNATURE-----
--Signature=_Mon__14_Nov_2005_03_26_51_+0200_y8O2hxiFumJNFJbG--
------------------------------
Date: Mon, 14 Nov 2005 01:30:57 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: wow shit! I ran into troubles with threads
Message-Id: <Xns970DD0B55DC10asu1cornelledu@127.0.0.1>
"Serge A. Ribalchenko" <fisherNOPASARAN@uch.net> wrote in
news:20051114032651.5b1960ff.fisherNOPASARAN@uch.net:
> On Mon, 14 Nov 2005 00:18:33 GMT
> "A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
>
>> > Re: wow shit! I ran into troubles with threads
>>
>> This is a family group. I think most of us would appreciate it if you
>> kept your language clean.
>
> so I decided that it's up to your culture. (or maybe absence of ?..)
My culture has nothing to do with your rudeness.
> Try to talk with me in my native language,
Maybe you should try talking to me in my native language.
...
> -e $LockFile && do {
> print "Another incarnation of mine is running.\n"; exit 1; };
You are creating a race condition here.
See:
perldoc -q lock
perldoc -f flock
Bye.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: Mon, 14 Nov 2005 03:44:35 +0200
From: "Serge A. Ribalchenko" <fisherNOPASARAN@uch.net>
Subject: Re: wow shit! I ran into troubles with threads
Message-Id: <20051114034435.6f666960.fisherNOPASARAN@uch.net>
On Mon, 14 Nov 2005 01:30:57 GMT
"A. Sinan Unur" <1usa@llenroc.ude.invalid> wrote:
> > so I decided that it's up to your culture. (or maybe absence of ?..)
>
> My culture has nothing to do with your rudeness.
Excuse me :(
--
Best wishes,
~ Serge. pubkeys: http://uch.net/~fisher/keys.asc
~fingerprint : 4346 2766 BC96 E77F 5BFF 1E7C 12C2 3852 E5FD DC34
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 8663
***************************************