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()

from tilequeue.tile import serialize_coord, deserialize_coord, CoordMessage
import threading


class OutputFileQueue(object):

    def __init__(self, fp):
        self.fp = fp
        self.lock = threading.RLock()

    def enqueue(self, coord):
        with self.lock:
            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):
        with self.lock:
            coords = []
            for _ in range(max_to_read):
                try:
                    coord = next(self.fp)
                except StopIteration:
                    break
                coords.append(CoordMessage(deserialize_coord(coord),
                                           None, None))

        return coords

    def job_done(self, coord_message):
        pass

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

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

Add multithreaded support to queue.file