Skip to content

Add async task background worker #4591

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

Draft
wants to merge 10 commits into
base: srothh/worker-class-hierarchy
Choose a base branch
from

Conversation

srothh
Copy link
Member

@srothh srothh commented Jul 17, 2025

Add a new implementation of the transport background worker based on an async task. This worker mostly mirrors the same functionality as the thread-based worker, with the exception that it exposes a non-blocking async flush. Furthermore, the worker itself is not thread-safe and should be called using run_coroutine_threadsafe or similar when called from another thread.

GH-4581

srothh added 6 commits July 17, 2025 11:41
Add a new implementation of the worker interface, implementing the worker as an async task. This is
to be used by the upcoming async transport.

GH-4581
…orker

Merge changes of worker/transport ABC structure
Refactor the flush method in the async worker to use the async_flush coroutine.

GH-4581
…orker

Adopt class hierarchy changes from worker hierarchy
…unctions

Add a check to see wheter callbacks are awaitable coroutines or functions, as coroutines need to be awaited.

GH-4581
…coroutines

Coroutines do not return None, therefore it is necessary to consider this in the callback parameter of the worker. Previously,
only callbacks with return Type None were accepted.

GH-4581
Copy link

codecov bot commented Jul 17, 2025

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
1618 1 1617 18
View the top 1 failed test(s) by shortest run time
tests.integrations.sanic.test_sanic::test_transactions[test_config4]
Stack Traces | 0.161s run time
.../integrations/sanic/test_sanic.py:423: in test_transactions
    with c as client:
         ^
.tox/py3.13-sanic-latest/lib/python3.13........./site-packages/sanic_testing/reusable.py:65: in __enter__
    self.run()
.tox/py3.13-sanic-latest/lib/python3.13........./site-packages/sanic_testing/reusable.py:77: in run
    self._server = self._run(self._server_co)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
.tox/py3.13-sanic-latest/lib/python3.13........./site-packages/sanic_testing/reusable.py:198: in _run
    return self._loop.run_until_complete(coro)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.../hostedtoolcache/Python/3.13.5....../x64/lib/python3.13/asyncio/base_events.py:725: in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
.tox/py3.13-sanic-latest/lib/python3.13.../sanic/mixins/startup.py:604: in create_server
    return await serve(
.tox/py3.13-sanic-latest/lib/python3.13.../sanic/server/async_server.py:113: in __await__
    self.server = task.result()
                  ^^^^^^^^^^^^^
.../hostedtoolcache/Python/3.13.5....../x64/lib/python3.13/asyncio/base_events.py:1622: in create_server
    raise OSError(err.errno, msg) from None
E   OSError: [Errno 98] error while attempting to bind on address ('127.0.0.1', 56784): [errno 98] address already in use

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

srothh added 4 commits July 17, 2025 15:53
Enable concurrent callbacks on async task worker by firing them as a task rather than awaiting them. A done callback handles the necessary queue and exception logic.

GH-4581
Changed kill to also use the _TERMINATOR sentinel, so the queue is still drained to this point on kill instead of cancelled immediately. This should also fix potential race conditions with flush_async.

GH-4581
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant