Skip to content

Commit

Permalink
Merge pull request #327 from Lorak-mmk/fix-asyncioconnection
Browse files Browse the repository at this point in the history
AsyncioConnection: fix initialize_reactor when called in event loop
  • Loading branch information
avelanarius authored Jun 12, 2024
2 parents c51d4cc + dedf571 commit aa8ba2e
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions cassandra/io/asyncioreactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,17 @@ def __init__(self, *args, **kwargs):
def initialize_reactor(cls):
with cls._lock:
if cls._pid != os.getpid():
# This means that class was passed to another process,
# e.g. using multiprocessing.
# In such case the class instance will be different and passing
# tasks to loop thread won't work.
# To fix we need to re-initialize the class
cls._loop = None
cls._loop_thread = None
cls._pid = os.getpid()
if cls._loop is None:
try:
cls._loop = asyncio.get_running_loop()
except RuntimeError:
cls._loop = asyncio.new_event_loop()
asyncio.set_event_loop(cls._loop)

if not cls._loop_thread:
assert cls._loop_thread is None
cls._loop = asyncio.new_event_loop()
# daemonize so the loop will be shut down on interpreter
# shutdown
cls._loop_thread = Thread(target=cls._loop.run_forever,
Expand Down

0 comments on commit aa8ba2e

Please sign in to comment.