prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Clusters view still very heavy
  I uploaded the code to a new server, following #463.
  The basic markers view is better and faster, but the clusters is still heavy and might cause server failure (I think the query is hogging the DB).
  
  </issue>
  <code>
  [start of clusters_calculator.py]
  1 from models import Marker
  2 from static.pymapcluster import calculate_clusters
  3 import logging
  4 import concurrent.futures
  5 import multiprocessing
  6 
  7 
  8 def retrieve_clusters(**kwargs):
  9     marker_boxes = divide_to_boxes(kwargs['ne_lat'], kwargs['ne_lng'], kwargs['sw_lat'], kwargs['sw_lng'])
  10     result_futures = []
  11     logging.info('number of cores: ' + str(multiprocessing.cpu_count()))
  12     with concurrent.futures.ThreadPoolExecutor(max_workers=multiprocessing.cpu_count()) as executor:
  13         for marker_box in marker_boxes:
  14 
  15             kwargs.update(marker_box)
  16             markers_in_box = Marker.bounding_box_query(**kwargs).markers.all()
  17             result_futures.append(executor.submit(calculate_clusters, markers_in_box, kwargs['zoom']))
  18 
  19     completed_futures = concurrent.futures.wait(result_futures)
  20     result = []
  21     for future in completed_futures.done:
  22         result.extend(future.result())
  23 
  24     return result
  25 
  26 
  27 def divide_to_boxes(ne_lat, ne_lng, sw_lat, sw_lng):
  28     cpu_count = multiprocessing.cpu_count()
  29     lat_box_size = (ne_lat - sw_lat) / cpu_count
  30     # lng_box_size = (sw_lng - ne_lng) / cpu_count
  31     boxes = []
  32     for i in xrange(cpu_count):
  33         # TODO: the below calculation is using sw_lat as first param instead of ne_lat. Plz verify my fix for that:
  34         # boxes.append((sw_lat + (i + 1) * lat_box_size, ne_lng, sw_lat + i * lat_box_size, sw_lng))
  35         boxes.append({'ne_lat': ne_lat + (i + 1) * lat_box_size, 'ne_lng': ne_lng,
  36                       'sw_lat': sw_lat + i * lat_box_size, 'sw_lng': sw_lng})
  37 
  38     return boxes
  39
  [end of clusters_calculator.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/clusters_calculator.py b/clusters_calculator.py
  --- a/clusters_calculator.py
  +++ b/clusters_calculator.py
  @@ -1,27 +1,25 @@
  +import itertools
  +from celery import Celery, group
   from models import Marker
   from static.pymapcluster import calculate_clusters
  -import logging
  -import concurrent.futures
   import multiprocessing
   
   
  -def retrieve_clusters(**kwargs):
  -    marker_boxes = divide_to_boxes(kwargs['ne_lat'], kwargs['ne_lng'], kwargs['sw_lat'], kwargs['sw_lng'])
  -    result_futures = []
  -    logging.info('number of cores: ' + str(multiprocessing.cpu_count()))
  -    with concurrent.futures.ThreadPoolExecutor(max_workers=multiprocessing.cpu_count()) as executor:
  -        for marker_box in marker_boxes:
  +celery_app = Celery('tasks', backend='rpc://', broker='pyamqp://guest@localhost//')
   
  -            kwargs.update(marker_box)
  -            markers_in_box = Marker.bounding_box_query(**kwargs).markers.all()
  -            result_futures.append(executor.submit(calculate_clusters, markers_in_box, kwargs['zoom']))
  +@celery_app.task
  +def calculate_marker_box(kwargs, marker_box):
  +    kwargs.update(marker_box)
  +    markers_in_box = Marker.bounding_box_query(**kwargs).markers.all()
  +    return calculate_clusters(markers_in_box, kwargs['zoom'])
   
  -    completed_futures = concurrent.futures.wait(result_futures)
  -    result = []
  -    for future in completed_futures.done:
  -        result.extend(future.result())
   
  -    return result
  +def retrieve_clusters(**kwargs):
  +    marker_boxes = divide_to_boxes(kwargs['ne_lat'], kwargs['ne_lng'], kwargs['sw_lat'], kwargs['sw_lng'])
  +    job = group([calculate_marker_box.s(kwargs, marker_box) for marker_box in marker_boxes])
  +    result = job.apply_async()
  +    result.join()
  +    return list(itertools.chain.from_iterable(result.get()))
   
   
   def divide_to_boxes(ne_lat, ne_lng, sw_lat, sw_lng):
  
verification_info: {"golden_diff": "diff --git a/clusters_calculator.py b/clusters_calculator.py\n--- a/clusters_calculator.py\n+++ b/clusters_calculator.py\n@@ -1,27 +1,25 @@\n+import itertools\n+from celery import Celery, group\n from models import Marker\n from static.pymapcluster import calculate_clusters\n-import logging\n-import concurrent.futures\n import multiprocessing\n \n \n-def retrieve_clusters(**kwargs):\n-    marker_boxes = divide_to_boxes(kwargs['ne_lat'], kwargs['ne_lng'], kwargs['sw_lat'], kwargs['sw_lng'])\n-    result_futures = []\n-    logging.info('number of cores: ' + str(multiprocessing.cpu_count()))\n-    with concurrent.futures.ThreadPoolExecutor(max_workers=multiprocessing.cpu_count()) as executor:\n-        for marker_box in marker_boxes:\n+celery_app = Celery('tasks', backend='rpc://', broker='pyamqp://guest@localhost//')\n \n-            kwargs.update(marker_box)\n-            markers_in_box = Marker.bounding_box_query(**kwargs).markers.all()\n-            result_futures.append(executor.submit(calculate_clusters, markers_in_box, kwargs['zoom']))\n+@celery_app.task\n+def calculate_marker_box(kwargs, marker_box):\n+    kwargs.update(marker_box)\n+    markers_in_box = Marker.bounding_box_query(**kwargs).markers.all()\n+    return calculate_clusters(markers_in_box, kwargs['zoom'])\n \n-    completed_futures = concurrent.futures.wait(result_futures)\n-    result = []\n-    for future in completed_futures.done:\n-        result.extend(future.result())\n \n-    return result\n+def retrieve_clusters(**kwargs):\n+    marker_boxes = divide_to_boxes(kwargs['ne_lat'], kwargs['ne_lng'], kwargs['sw_lat'], kwargs['sw_lng'])\n+    job = group([calculate_marker_box.s(kwargs, marker_box) for marker_box in marker_boxes])\n+    result = job.apply_async()\n+    result.join()\n+    return list(itertools.chain.from_iterable(result.get()))\n \n \n def divide_to_boxes(ne_lat, ne_lng, sw_lat, sw_lng):\n", "issue": "Clusters view still very heavy\nI uploaded the code to a new server, following #463.\r\nThe basic markers view is better and faster, but the clusters is still heavy and might cause server failure (I think the query is hogging the DB).\n", "before_files": [{"content": "from models import Marker\nfrom static.pymapcluster import calculate_clusters\nimport logging\nimport concurrent.futures\nimport multiprocessing\n\n\ndef retrieve_clusters(**kwargs):\n    marker_boxes = divide_to_boxes(kwargs['ne_lat'], kwargs['ne_lng'], kwargs['sw_lat'], kwargs['sw_lng'])\n    result_futures = []\n    logging.info('number of cores: ' + str(multiprocessing.cpu_count()))\n    with concurrent.futures.ThreadPoolExecutor(max_workers=multiprocessing.cpu_count()) as executor:\n        for marker_box in marker_boxes:\n\n            kwargs.update(marker_box)\n            markers_in_box = Marker.bounding_box_query(**kwargs).markers.all()\n            result_futures.append(executor.submit(calculate_clusters, markers_in_box, kwargs['zoom']))\n\n    completed_futures = concurrent.futures.wait(result_futures)\n    result = []\n    for future in completed_futures.done:\n        result.extend(future.result())\n\n    return result\n\n\ndef divide_to_boxes(ne_lat, ne_lng, sw_lat, sw_lng):\n    cpu_count = multiprocessing.cpu_count()\n    lat_box_size = (ne_lat - sw_lat) / cpu_count\n    # lng_box_size = (sw_lng - ne_lng) / cpu_count\n    boxes = []\n    for i in xrange(cpu_count):\n        # TODO: the below calculation is using sw_lat as first param instead of ne_lat. Plz verify my fix for that:\n        # boxes.append((sw_lat + (i + 1) * lat_box_size, ne_lng, sw_lat + i * lat_box_size, sw_lng))\n        boxes.append({'ne_lat': ne_lat + (i + 1) * lat_box_size, 'ne_lng': ne_lng,\n                      'sw_lat': sw_lat + i * lat_box_size, 'sw_lng': sw_lng})\n\n    return boxes\n", "path": "clusters_calculator.py"}]}