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
In reply to Threading and File Handles by matt.schnarr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |