Skip to content

Commit d407423

Browse files
committed
asyncio: stop using the loop variable when not needed
there are some places were we don't need to pass or create the asyncio loop, and we should avoid it
1 parent 64f3fe9 commit d407423

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

cassandra/io/asyncioreactor.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import threading
2+
13
from cassandra.connection import Connection, ConnectionShutdown
24
import sys
35
import asyncio
@@ -41,13 +43,12 @@ def end(self):
4143

4244
def __init__(self, timeout, callback, loop):
4345
delayed = self._call_delayed_coro(timeout=timeout,
44-
callback=callback,
45-
loop=loop)
46+
callback=callback)
4647
self._handle = asyncio.run_coroutine_threadsafe(delayed, loop=loop)
4748

4849
@staticmethod
49-
async def _call_delayed_coro(timeout, callback, loop):
50-
await asyncio.sleep(timeout, loop=loop)
50+
async def _call_delayed_coro(timeout, callback):
51+
await asyncio.sleep(timeout)
5152
return callback()
5253

5354
def __lt__(self, other):
@@ -111,8 +112,11 @@ def initialize_reactor(cls):
111112
if cls._pid != os.getpid():
112113
cls._loop = None
113114
if cls._loop is None:
114-
cls._loop = asyncio.new_event_loop()
115-
asyncio.set_event_loop(cls._loop)
115+
try:
116+
cls._loop = asyncio.get_running_loop()
117+
except RuntimeError:
118+
cls._loop = asyncio.new_event_loop()
119+
asyncio.set_event_loop(cls._loop)
116120

117121
if not cls._loop_thread:
118122
# daemonize so the loop will be shut down on interpreter
@@ -165,7 +169,7 @@ def push(self, data):
165169
else:
166170
chunks = [data]
167171

168-
if self._loop_thread.ident != get_ident():
172+
if self._loop_thread != threading.current_thread():
169173
asyncio.run_coroutine_threadsafe(
170174
self._push_msg(chunks),
171175
loop=self._loop

0 commit comments

Comments
 (0)