We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN 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).

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `clusters_calculator.py`
Content:
```
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 FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

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

{"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"}], "after_files": [{"content": "import itertools\nfrom celery import Celery, group\nfrom models import Marker\nfrom static.pymapcluster import calculate_clusters\nimport multiprocessing\n\n\ncelery_app = Celery('tasks', backend='rpc://', broker='pyamqp://guest@localhost//')\n\n@celery_app.task\ndef 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\ndef 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\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"}]}