Skip to content

Removing loop name from asyncio.create_task #489

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

Merged
merged 23 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ydb/_topic_common/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import concurrent.futures
import sys
import threading
import typing
from typing import Optional
Expand Down Expand Up @@ -29,6 +30,12 @@ def wrapper(rpc_state, response_pb, driver=None):
return wrapper


def wrap_create_asyncio_task(func: typing.Callable, task_name: str, *args, **kwargs):
if sys.hexversion < 0x03080000:
return asyncio.create_task(func(*args, **kwargs))
return asyncio.create_task(func(*args, **kwargs), name=task_name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems weird how we pass name and args. This wrapper could get already created task and use task.set_name(name) in case of python 3.8+. In this case you will don't have any problems between asyncio.create_task() and loop.create_task() because they both return a Task you can pass into your wrapper

Also this wrapper could have a better name, because the current one doesn't explain anything we do with the task.

I don't have anything against hexversion, but I'm sure we can omit this check on every wrapper call. We could use this split during function declaration stage. In this case, if we will have only one if call, think about switching to version_info for better readability (i'm not insist here)



_shared_event_loop_lock = threading.Lock()
_shared_event_loop: Optional[asyncio.AbstractEventLoop] = None

Expand Down
21 changes: 16 additions & 5 deletions ydb/_topic_reader/topic_reader_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import asyncio
import concurrent.futures
import gzip
import sys
import typing
from asyncio import Task
from collections import deque
from typing import Optional, Set, Dict, Union, Callable

import ydb
from .. import _apis, issues
from .._topic_common import common as topic_common
from .._utilities import AtomicCounter
from ..aio import Driver
from ..issues import Error as YdbError, _process_response
Expand Down Expand Up @@ -87,7 +89,10 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):

def __del__(self):
if not self._closed:
self._loop.create_task(self.close(flush=False), name="close reader")
if sys.hexversion < 0x03080000:
self._loop.create_task(self.close(flush=False))
else:
self._loop.create_task(self.close(flush=False), name="close reader")

async def wait_message(self):
"""
Expand Down Expand Up @@ -337,12 +342,18 @@ async def _start(self, stream: IGrpcWrapperAsyncIO, init_message: StreamReadMess

self._update_token_event.set()

self._background_tasks.add(asyncio.create_task(self._read_messages_loop(), name="read_messages_loop"))
self._background_tasks.add(asyncio.create_task(self._decode_batches_loop(), name="decode_batches"))
self._background_tasks.add(
topic_common.wrap_create_asyncio_task(self._read_messages_loop, "read_messages_loop"),
)
self._background_tasks.add(
topic_common.wrap_create_asyncio_task(self._decode_batches_loop, "decode_batches"),
)
if self._get_token_function:
self._background_tasks.add(asyncio.create_task(self._update_token_loop(), name="update_token_loop"))
self._background_tasks.add(
topic_common.wrap_create_asyncio_task(self._update_token_loop, "update_token_loop"),
)
self._background_tasks.add(
asyncio.create_task(self._handle_background_errors(), name="handle_background_errors")
topic_common.wrap_create_asyncio_task(self._handle_background_errors, "handle_background_errors"),
)

async def wait_error(self):
Expand Down
18 changes: 13 additions & 5 deletions ydb/_topic_writer/topic_writer_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
issues,
)
from .._errors import check_retriable_error
from .._topic_common import common as topic_common
from ..retries import RetrySettings
from .._grpc.grpcwrapper.ydb_topic_public_types import PublicCodec
from .._grpc.grpcwrapper.ydb_topic import (
Expand Down Expand Up @@ -231,8 +232,8 @@ def __init__(self, driver: SupportedDriverType, settings: WriterSettings):
self._new_messages = asyncio.Queue()
self._stop_reason = self._loop.create_future()
self._background_tasks = [
asyncio.create_task(self._connection_loop(), name="connection_loop"),
asyncio.create_task(self._encode_loop(), name="encode_loop"),
topic_common.wrap_create_asyncio_task(self._connection_loop, "connection_loop"),
topic_common.wrap_create_asyncio_task(self._encode_loop, "encode_loop"),
]

self._state_changed = asyncio.Event()
Expand Down Expand Up @@ -366,8 +367,12 @@ async def _connection_loop(self):

self._stream_connected.set()

send_loop = asyncio.create_task(self._send_loop(stream_writer), name="writer send loop")
receive_loop = asyncio.create_task(self._read_loop(stream_writer), name="writer receive loop")
send_loop = topic_common.wrap_create_asyncio_task(self._send_loop, "writer send loop", stream_writer)
receive_loop = topic_common.wrap_create_asyncio_task(
self._read_loop,
"writer receive loop",
stream_writer,
)

tasks = [send_loop, receive_loop]
done, _ = await asyncio.wait([send_loop, receive_loop], return_when=asyncio.FIRST_COMPLETED)
Expand Down Expand Up @@ -653,7 +658,10 @@ async def _start(self, stream: IGrpcWrapperAsyncIO, init_message: StreamWriteMes

if self._update_token_interval is not None:
self._update_token_event.set()
self._update_token_task = asyncio.create_task(self._update_token_loop(), name="update_token_loop")
self._update_token_task = topic_common.wrap_create_asyncio_task(
self._update_token_loop,
"update_token_loop",
)

@staticmethod
def _ensure_ok(message: WriterMessagesFromServerToClient):
Expand Down
Loading