from tilequeue.tile import serialize_coord


class OutputFileQueue(object):

    def __init__(self, fp):
        self.fp = fp

    def enqueue(self, coord):
        payload = serialize_coord(coord)
        self.fp.write(payload + '\n')

    def enqueue_batch(self, coords):
        n = 0
        for coord in coords:
            self.enqueue(coord)
            n += 1
        return n, 0

    def read(self, max_to_read=1, timeout_seconds=20):
        coords = []
        for _ in range(max_to_read):
            try:
                coords.append(next(self.fp))
            except StopIteration:
                break

        return coords

    def job_done(self, coord_message):
        pass

    def clear(self):
        self.fp.seek(0)
        self.fp.truncate()
        return -1

    def close(self):
        remaining_queue = "".join([ln for ln in self.fp])
        self.clear()
        self.fp.write(remaining_queue)
        self.fp.close()

@@ -35,16 +35,66 @@
 ze_coord
+, deserialize_coord, CoordMessage%0Aimport threading
 %0A%0A%0Aclass
@@ -168,16 +168,54 @@
 .fp = fp
+%0A        self.lock = threading.RLock()
 %0A%0A    de
@@ -234,24 +234,52 @@
 lf, coord):%0A
+        with self.lock:%0A    
         payl
@@ -299,32 +299,36 @@
 ze_coord(coord)%0A
+    
         self.fp.
@@ -557,16 +557,44 @@
 ds=20):%0A
+        with self.lock:%0A    
         
@@ -605,16 +605,20 @@
 ds = %5B%5D%0A
+    
         
@@ -662,13 +662,21 @@
     
+ 
+   
 try:%0A
+    
     
@@ -696,17 +696,11 @@
 oord
-s.append(
+ = 
 next
@@ -708,18 +708,21 @@
 self.fp)
-)
 %0A
+    
         
@@ -767,13 +767,142 @@
     
+    
 break
+%0A                coords.append(CoordMessage(deserialize_coord(coord),%0A                                           None, None))
 %0A%0A  
@@ -988,32 +988,60 @@
 ef clear(self):%0A
+        with self.lock:%0A    
         self.fp.
@@ -1048,32 +1048,36 @@
 seek(0)%0A        
+    
 self.fp.truncate
@@ -1079,16 +1079,20 @@
 ncate()%0A
+    
         
@@ -1115,32 +1115,60 @@
 ef close(self):%0A
+        with self.lock:%0A    
         remainin
@@ -1209,16 +1209,20 @@
 lf.fp%5D)%0A
+    
         
@@ -1234,32 +1234,36 @@
 clear()%0A        
+    
 self.fp.write(re
@@ -1277,16 +1277,20 @@
 _queue)%0A
+    
         

Add multithreaded support to queue.file