Skip to content

Commit 9c19b75

Browse files
committed
more fixes to asyncio
1 parent f204a92 commit 9c19b75

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

cassandra/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ def _connect_socket(self):
940940
self._initiate_connection(sockaddr)
941941
self._socket.settimeout(None)
942942
local_addr = self._socket.getsockname()
943-
log.debug('Connection %s %s:%s -> %s:%s', id(self), local_addr[0], local_addr[1], sockaddr[0], sockaddr[1])
943+
# log.debug('Connection %s %s:%s -> %s:%s', id(self), local_addr[0], local_addr[1], sockaddr[0], sockaddr[1])
944944
if self._check_hostname:
945945
self._match_hostname()
946946
sockerr = None

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)