Skip to content

Commit

Permalink
add nest_asyncio to deal with multiple event loops
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelchin committed Nov 14, 2023
1 parent d385ff7 commit d3e0c56
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,9 @@ The examples of using PlateModelManager with GPlately:
- https://github.com/GPlates/gplately/blob/master/Notebooks/Examples/working-with-plate-model-manager.py

The PlateModelManager can also be used with the GPlates desktop. Use the command line to download the plate model files and open the files with GPlates desktop. This will save the trouble of downloading files from Internet manually.

### Dependencies

- aiohttp
- requests
- nest_asyncio
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "plate-model-manager"
version = "1.1.0"
version = "1.1.1"
description = "Plate Model Manager"
readme = "README.md"
authors = [{ name = "Michael Chin", email = "michael.chin@sydney.edu.au" }]
Expand All @@ -18,6 +18,7 @@ keywords = ["Tectonics", "Earth"]
dependencies = [
"aiohttp",
"requests",
"nest_asyncio",
]
requires-python = ">=3.8"

Expand Down
8 changes: 6 additions & 2 deletions src/plate_model_manager/network_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async def _fetch_file(
):
"""async "fetch_file" implementation. See the docstring of "fetch_file" """

if etag:
if isinstance(etag, str) or isinstance(etag, bytes):
headers = {"If-None-Match": etag}
else:
headers = {}
Expand Down Expand Up @@ -206,7 +206,11 @@ async def f():
new_etags = []
try:
new_etags = loop.run_until_complete(f())
# print(new_etags)
except RuntimeError:
import nest_asyncio

nest_asyncio.apply()
new_etags = loop.run_until_complete(f())
finally:
loop.close()
return new_etags
Expand Down
15 changes: 14 additions & 1 deletion src/plate_model_manager/network_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def fetch_file(
"""

if etag:
if isinstance(etag, str) or isinstance(etag, bytes):
headers = {"If-None-Match": etag}
else:
headers = {}
Expand Down Expand Up @@ -203,6 +203,19 @@ def fetch_files(
auto_unzip=auto_unzip,
)
)
except RuntimeError:
import nest_asyncio

nest_asyncio.apply()
loop.run_until_complete(
self._async_fetch_files(
run,
urls,
filepaths,
etags=etags,
auto_unzip=auto_unzip,
)
)
finally:
loop.close()

Expand Down
16 changes: 14 additions & 2 deletions src/plate_model_manager/plate_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@ async def f():
# print(tasks)
await asyncio.wait(tasks)

self.loop.run_until_complete(f())
try:
self.loop.run_until_complete(f())
except RuntimeError:
import nest_asyncio

nest_asyncio.apply()
self.loop.run_until_complete(f())

def get_avail_time_dependent_raster_names(self):
"""return the names of all time dependent rasters which have been configurated in this model."""
Expand Down Expand Up @@ -345,7 +351,13 @@ async def f():
# print(tasks)
await asyncio.wait(tasks)

self.loop.run_until_complete(f())
try:
self.loop.run_until_complete(f())
except RuntimeError:
import nest_asyncio

nest_asyncio.apply()
self.loop.run_until_complete(f())

else:
raise Exception(
Expand Down
83 changes: 83 additions & 0 deletions unittest/test_nest_asyncio.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "2c3659e1-ede1-4a79-b0bb-5346c5336300",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"downloading https://www.earthbyte.org/webdav/ftp/Data_Collections/Muller_etal_2019_Tectonics/Muller_etal_2019_Agegrids/Muller_etal_2019_Tectonics_v2.0_netCDF/Muller_etal_2019_Tectonics_v2.0_AgeGrid-0.ncdownloading https://www.earthbyte.org/webdav/ftp/Data_Collections/Muller_etal_2019_Tectonics/Muller_etal_2019_Agegrids/Muller_etal_2019_Tectonics_v2.0_netCDF/Muller_etal_2019_Tectonics_v2.0_AgeGrid-1.nc\n",
"\n",
"The local file(s) is/are still good. Will not download again at this moment.\n",
"The local file(s) is/are still good. Will not download again at this moment.\n",
"downloading https://repo.gplates.org/webdav/pmm/muller2019/Coastlines.zip\n",
"downloading https://repo.gplates.org/webdav/pmm/muller2019/Rotations.zip\n",
"downloading https://repo.gplates.org/webdav/pmm/muller2019/ContinentalPolygons.zip\n",
"downloading https://repo.gplates.org/webdav/pmm/muller2019/StaticPolygons.zip\n",
"downloading https://repo.gplates.org/webdav/pmm/muller2019/Topologies.zip\n",
"downloading https://repo.gplates.org/webdav/pmm/muller2019/COBs.zip\n",
"The local file(s) is/are still good. Will not download again at this moment.\n",
"The local file(s) is/are still good. Will not download again at this moment.\n",
"The local file(s) is/are still good. Will not download again at this moment.\n",
"The local file(s) is/are still good. Will not download again at this moment.\n",
"The local file(s) is/are still good. Will not download again at this moment.\n",
"The local file(s) is/are still good. Will not download again at this moment.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/mchin/workspace/plate-model-manager.git/src/plate_model_manager/plate_model.py:360: RuntimeWarning: coroutine 'PlateModel.download_time_dependent_rasters.<locals>.f' was never awaited\n"
]
}
],
"source": [
"#import nest_asyncio\n",
"#nest_asyncio.apply()\n",
"#you need to uncomment the two lines above if you don't what to see a warning\n",
"\n",
"import sys,os\n",
"sys.path.insert(0, f\"/Users/mchin/workspace/plate-model-manager.git/src\")\n",
"from plate_model_manager import PlateModelManager\n",
"pm_manager = PlateModelManager()\n",
"muller2019_model = pm_manager.get_model(\"Muller2019\", data_dir=\"plate-model-repo\")\n",
"muller2019_model.get_rasters(\"AgeGrids\", [0,1])\n",
"muller2019_model.download_all_layers()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "47900e7f-4074-4834-b6e5-30e30b828b8c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit d3e0c56

Please sign in to comment.