in reply to (Ovid) Re: CGI and Sleep?
in thread CGI and Sleep?

I'm almost ashamed to show how amature my code is, but here it is...
#!/usr/bin/perl -w close STDOUT; @playlist = `cat ./playlist.txt`; $totalnum = scalar(@playlist); foreach $i (@playlist) { # a random number for the randomizer. $random = int(rand $totalnum); # use that random number to determine a file to play. $filename = $playlist[$random]; # format the file name so I can *USE* it. # the first regex escapes symbols for me. the second, spaces. $filename =~ s/(')|(&)|(\()|(\))/\\$+/g; $filename =~ s/\s/\\\ /g; # play the file, throw that process into the bg system("/usr/local/bin/ogg123 -d oss -q $filename &"); # write the PID and filename to a file # need this sleep in here to make shure the player starts before I t +ry # to write the PID sleep 2; system("ps -U root --format pid --format fname| grep ogg123> ./playe +r.pid"); open(PLAYING, ">./nowplaying.txt"); print PLAYING $filename; close PLAYING; # decriment the total number of files left to play. $totalnum--; # remove the filename from the list. splice @playlist, $random, 1; # get the information for the file that is playing. $playtime = `ogginfo $filename| grep length=`; # get the total playtime in secs for the file. $playtime =~ s/length=//; # pause for that many seconds, as to not play multiple files # simultaneously :) sleep $time; }

I shut off STDOUT so the CGI calling this script won't wait for this to execute.

Replies are listed 'Best First'.
Re: Re: (Ovid) Re: CGI and Sleep?
by chromatic (Archbishop) on Jun 26, 2001 at 06:35 UTC
    This may be a typo. strict would have caught it.

    sleep $playtime;

    Here's a very quick rewrite. It's untested, but it's a little nicer, in my opinion.

    #!/usr/bin/perl -w use strict; close STDOUT; open(LIST, 'playlist.txt') or die "Can't open playlist: $!"; my @playlist = <LIST>; close LIST; while (@playlist) { my $pos = rand @playlist; my $filename = playlist[$pos]; system("/usr/local/bin/ogg123", '-d', 'oss', 'q', $filename, '&'); sleep 2; foreach (`ps -U root --format pid fname`) { next unless /ogg123/; open(PID, '>player.pid') or warn "Can't log pid ($_): $!"; print PID $_; close PID; last; } open(PLAYING, ">nowplaying.txt") or warn "Can't log filename $file +name: $!"; print PLAYING $filename; close PLAYING; splice @playlist, $pos, 1; my $playtime; foreach (`ogginfo $filename`) next unless /length=(.+)$/; $playtime = $1; last; } sleep $playtime; }