selecting from a number of different input sources.by jasoncollins (Novice)
|on Jul 21, 2010 at 03:54 UTC||Need Help??|
jasoncollins has asked for the
wisdom of the Perl Monks concerning the following question:
Hi, My script reads from two text input files, each one of these text files gets updated every once in a while. I am suppose to look for a pattern in these input files and when I find a pattern I am suppose to print it to a socket.
Problem is that if you try to read from a file which has no new data in it, then the io->getline() function will get blocked, and you won't be able to read from the other file even if it is getting populated with valid data.
I have been looking at different solutions, but in every solution I seem to hit a roadblock, so please help!
Solution 1: Use IO::Select, and IO::Handle: In this solution I can select a file descriptor which is ready to be read from using:
from what I have read, it is not a good idea to mix, IO::Select() and IO based getline() functions, as then select gets confused and tells you there is data to be read, when in reality there is no data to be read (because IO has its own buffers), causing all sorts of issues. Is that correct? Has anyone successfully used getline and IO::Select successfully? Note, I don't want to use sysread because I am interested in getting an entire line and not bytes.
Solution 2: I looked in a multi-threaded solution: This was so that I won't have to rely on Select and each thread could maintain its own file handles and won't get blocked. But as it turns out, in Perl threads filehandles are shared across threads by default. So now: - I create a file handles for each input file. - I create two threads to process the data in isolation. - when initiating the function call to the threaded function, I pass the file descriptors as well, so that the same function can work with both file descriptors:
problem is that file descriptors are are shared by default among all threads! so me passing file descriptors to foo does no good, as foo already has it as a global (and I read that passing file descriptors amongst threads is a bad idea). So I don't know how can I use different file descriptors within foo to read data from two separate files, and have each of those file descriptors not get blocked.Than you!