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

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

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):
                coord = self.fp.readline()
                if coord:
                    coords.append(CoordMessage(deserialize_coord(coord), None))
                else:
                    break

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

Use readline() instead of next() to detect changes.
Use readline() instead of next() to detect changes.

tilequeue/queue/file.py
	-`readline()` will pick up new lines appended to the file,
	whereas `next()` will not since the iterator will just hit
	`StopIteration` and stop generating new lines. Use `readline()`
	instead, then, since it might be desirable to append something
	to the queue file and have tilequeue detect the new input
	without having to restart.

  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):
+                 coord = self.fp.readline()
+                 if coord:
+                     coords.append(CoordMessage(deserialize_coord(coord), None))
-                 try:
+                 else:
-                     coord = next(self.fp)
-                 except StopIteration:
                      break
-                 coords.append(CoordMessage(deserialize_coord(coord), 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])
+             remaining_queue = ''.join([ln for ln in self.fp])
              self.clear()
              self.fp.write(remaining_queue)
              self.fp.close()
  
Use readline() instead of next() to detect changes.
## Code Before:
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))

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

## Instruction:
Use readline() instead of next() to detect changes.
## Code After:
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):
                coord = self.fp.readline()
                if coord:
                    coords.append(CoordMessage(deserialize_coord(coord), None))
                else:
                    break

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