Skip to content

Commit 6724e74

Browse files
committed
Asyncio fixes
1 parent dc157d1 commit 6724e74

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/sasquatchbackpack/sasquatch.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ def __init__(self, address: str) -> None:
5959
self.address = address
6060
self.model = redis.from_url(self.address)
6161

62-
self.loop = asyncio.new_event_loop()
62+
# Get Existing loop, create one if it doesn't exist
63+
try:
64+
self.loop = asyncio.get_running_loop()
65+
except RuntimeError:
66+
self.loop = asyncio.new_event_loop()
67+
68+
asyncio.set_event_loop(self.loop)
6369

6470
def store(self, key: str, item: str = "value") -> None:
6571
"""Store a key value pair in the provided redis server.

src/sasquatchbackpack/sources/usgs/commands.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,15 @@ def usgs_earthquake_data(
199199

200200
click.echo("Post mode enabled: Sending data...")
201201
click.echo(f"Querying redis at {backpack_dispatcher.redis.address}")
202-
loop = asyncio.new_event_loop()
202+
203+
# Get Existing loop, create one if it doesn't exist
204+
try:
205+
loop = asyncio.get_running_loop()
206+
except RuntimeError:
207+
loop = asyncio.new_event_loop()
208+
209+
asyncio.set_event_loop(loop)
210+
203211
loop.run_until_complete(backpack_dispatcher.direct_connect())
204212

205213
click.echo("complete")

0 commit comments

Comments
 (0)