<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-2">
</head>
<body bgcolor="#ffffff" text="#000000">
I try to write an iterator class:<br>
<br>
class FileIterator(object):<br>
    def __init__(self,file_object):<br>
        self.fin = file_object<br>
        # other initialization code here ...<br>
    def __iter__(self):<br>
        return self<br>
    def next(self):<br>
        # special code that reads from the file and converts to
Pythonic data...<br>
<br>
This iterator I want to use with regular binary file objects. My
requirement is to be able to create many iterators for the same file
object, and use them from many threads - which is not straightforward.
Of course I do not want to save file positions in the iterators, use
locking on the file, and call seek() every time I need to read from the
file. Instead of that, I try to re-open the file for each iteator. The
trivial way to do it is:<br>
<br>
class FileIterator(object):<br>
    def __init__(self,file_object):<br>
        self.fin = open(file_object.name,"rb")<br>
        self.fin.seek(file_object.tell())<br>
    ...<br>
<br>
However, it won't work for temporary files. Temporary files are just
file-like objects, and their name is '<fdopen>'. I guess I could
open a temporary file with os.open, and then use os.dup, but that is
low level stuff. Then I have to use os.* methods, and the file object
won't be iterable. tempfile.NamedTempFile is not better, because it
cannot be reopened under Windows.<br>
<br>
So the question is, how do I create my iterator so that:<br>
<ul>
  <li>under Windows and Unix</li>
  <li>many iterators can be used for the same underlying file, from
many threads</li>
  <li>with a normal binary file</li>
  <li>and also with temporary files</li>
</ul>
A workaround would be to create a temporary directory, and then create
real files inside the temporary directory. If there is no better way to
do it, I'll do that.<br>
<br>
Thanks<br>
<br>
   Laszlo<br>
<br>
<br>
</body>
</html>