Skip to content

APP-5362 : feat: integrate uvloop for improved asyncio performance #179

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 8 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions application_sdk/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import threading
from typing import Any, List, Sequence

import uvloop
from temporalio.types import CallableType
Comment on lines +11 to 12
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix potential thread safety issues with uvloop initialization.

Setting the event loop policy at module level can lead to race conditions when using threading. The worker creates daemon threads that run their own event loops, which could conflict with the global policy setting.

Move the uvloop initialization into the worker thread:

-asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

 class Worker:
     async def start(self, daemon: bool = False, *args: Any, **kwargs: Any) -> None:
         if daemon:
             worker_thread = threading.Thread(
-                target=lambda: asyncio.run(self.start(daemon=False)), daemon=True
+                target=lambda: self._run_worker_with_uvloop(), daemon=True
             )
             worker_thread.start()
             return

+    def _run_worker_with_uvloop(self):
+        asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
+        asyncio.run(self.start(daemon=False))

Also applies to: 18-19


from application_sdk.clients.temporal import TemporalClient
from application_sdk.common.logger_adaptors import get_logger

logger = get_logger(__name__)
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
Copy link
Member Author

Choose a reason for hiding this comment

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

Setting the event loop policy sets the event loop at all the asyncio instances in the application sdk and in the apps, hence decided to keep it here as worker is used in all the apps



class Worker:
Expand Down
Loading
Loading