-
Notifications
You must be signed in to change notification settings - Fork 543
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes to stabilize asyncioreactor for multiple versions of python #1189
base: master
Are you sure you want to change the base?
Changes from all commits
d498787
24fc0e4
c0c7873
0ff2b2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
from cassandra.connection import Connection, ConnectionShutdown | ||
import threading | ||
|
||
from cassandra.connection import Connection, ConnectionShutdown | ||
import sys | ||
import asyncio | ||
import logging | ||
import os | ||
|
@@ -88,9 +90,11 @@ def __init__(self, *args, **kwargs): | |
|
||
self._connect_socket() | ||
self._socket.setblocking(0) | ||
|
||
self._write_queue = asyncio.Queue() | ||
self._write_queue_lock = asyncio.Lock() | ||
loop_args = dict() | ||
if sys.version_info[0] == 3 and sys.version_info[1] < 10: | ||
loop_args['loop'] = self._loop | ||
self._write_queue = asyncio.Queue(**loop_args) | ||
self._write_queue_lock = asyncio.Lock(**loop_args) | ||
|
||
# see initialize_reactor -- loop is running in a separate thread, so we | ||
# have to use a threadsafe call | ||
|
@@ -108,8 +112,11 @@ def initialize_reactor(cls): | |
if cls._pid != os.getpid(): | ||
cls._loop = None | ||
if cls._loop is None: | ||
cls._loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(cls._loop) | ||
try: | ||
cls._loop = asyncio.get_running_loop() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 get_running_loop() is preferred as the entry point over get_event_loop() per asyncio docs. No obvious reason it wouldn't be preferred to new_event_loop() + set_event_loop() as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @absurdfarce turns out that in the meantime in a fork this part was refined see code change at and respective explanation at |
||
except RuntimeError: | ||
cls._loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(cls._loop) | ||
|
||
if not cls._loop_thread: | ||
# daemonize so the loop will be shut down on interpreter | ||
|
@@ -162,7 +169,7 @@ def push(self, data): | |
else: | ||
chunks = [data] | ||
|
||
if self._loop_thread.ident != get_ident(): | ||
if self._loop_thread != threading.current_thread(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 Since |
||
asyncio.run_coroutine_threadsafe( | ||
self._push_msg(chunks), | ||
loop=self._loop | ||
|
@@ -173,7 +180,7 @@ def push(self, data): | |
|
||
async def _push_msg(self, chunks): | ||
# This lock ensures all chunks of a message are sequential in the Queue | ||
with await self._write_queue_lock: | ||
async with self._write_queue_lock: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
for chunk in chunks: | ||
self._write_queue.put_nowait(chunk) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This came up when we were working on PYTHON-1313. Docs I found at the time indicated that since Python 3.7 the event loop had been derived from the current threads event loop and the "loop" param was essentially ignored. We're currently looking at 3.8 through 3.12 so this block only applies to Python 3.8.x and 3.9.x. Do you think it's worth it to continue to pass a "loop" param for those cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it seems to be needed, I.e. taking it down got it broke.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1