matt.schnarr has asked for the wisdom of the Perl Monks concerning the following question:
This is my first attempt at using Threading and I've apparently stumpted myself. :(
I have a main body of code which will open a file handle and then I spawn a new thread which I would like to use to close this file handle that I've just opened outside the new thread.
Unfortunatley it doesn't seem like I can close the file handle in a different thread then which it was opened.
Does anyone have any ideas?
#!/usr/bin/perl use Thread; $filename = "/tmp/input.log"; $outputdir = "/var/log/"; $first_event = 1; $position = 0; $time = time(); # Define Timer sub timer { my $counter = 0; while (1) { sleep(1); if ($counter >= 60) { $counter = 0; # #I can't seem to close FILE from the main thre +ad... why? # close (FILE); $outputfile = $outputdir . "log-". time(); open(FILE, "> $outputfile") || die("Can't open + $outputfile to write to"); } $counter++; } } $thr1 = new Thread \&timer; # Spawn the thread open(TAIL, "tail --follow=name $filename|"); $outputfile = $outputdir . "log-". time(); open(FILE, "> $outputfile") || die("Can't open $outputfile to write to +"); while (<TAIL>) { # do some stuff and print to FILE }
Thanks! Matthew Schnarr
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Threading and File Handles
by BrowserUk (Patriarch) on Sep 01, 2004 at 13:59 UTC | |
by matt.schnarr (Sexton) on Sep 01, 2004 at 14:46 UTC | |
by BrowserUk (Patriarch) on Sep 01, 2004 at 16:11 UTC | |
by zentara (Cardinal) on Sep 02, 2004 at 12:53 UTC | |
|
Re: Threading and File Handles
by doowah2004 (Monk) on Sep 01, 2004 at 20:12 UTC |