-
Notifications
You must be signed in to change notification settings - Fork 486
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
An ability to access PubSubHub from outside Flet app #3446
Changes from 1 commit
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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -12,6 +12,7 @@ | |||||||||||||
from flet_core.connection import Connection | ||||||||||||||
from flet_core.locks import NopeLock | ||||||||||||||
from flet_core.page import Page | ||||||||||||||
from flet_core.pubsub.pubsub_hub import PubSubHub | ||||||||||||||
from flet_core.utils.concurrency_utils import is_pyodide | ||||||||||||||
|
||||||||||||||
logger = logging.getLogger(flet_fastapi.__name__) | ||||||||||||||
|
@@ -31,11 +32,26 @@ def __init__(self): | |||||||||||||
self.__evict_oauth_states_task = None | ||||||||||||||
self.__temp_dirs = {} | ||||||||||||||
self.__executor = ThreadPoolExecutor(thread_name_prefix="flet_fastapi") | ||||||||||||||
self.__pubsubhubs_lock = threading.Lock() | ||||||||||||||
self.__pubsubhubs = {} | ||||||||||||||
|
||||||||||||||
@property | ||||||||||||||
def executor(self): | ||||||||||||||
return self.__executor | ||||||||||||||
|
||||||||||||||
def get_pubsubhub( | ||||||||||||||
self, session_handler, loop: Optional[asyncio.AbstractEventLoop] = None | ||||||||||||||
): | ||||||||||||||
Comment on lines
+42
to
+44
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. suggestion: Add type hint for session_handler parameter To improve code readability and maintainability, consider adding a type hint for the session_handler parameter.
Suggested change
|
||||||||||||||
with self.__pubsubhubs_lock: | ||||||||||||||
psh = self.__pubsubhubs.get(session_handler, None) | ||||||||||||||
if psh is None: | ||||||||||||||
Comment on lines
+46
to
+47
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. issue (bug_risk): Potential race condition with pubsubhubs dictionary Although the lock should prevent race conditions, consider using a defaultdict to simplify the logic and reduce the risk of potential issues. |
||||||||||||||
psh = PubSubHub( | ||||||||||||||
loop=loop if loop else 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. suggestion (code-quality): Replace if-expression with
Suggested change
ExplanationHere we find ourselves setting a value if it evaluates toTrue , and otherwiseusing a default. The 'After' case is a bit easier to read and avoids the duplication of It works because the left-hand side is evaluated first. If it evaluates to |
||||||||||||||
executor=self.__executor, | ||||||||||||||
) | ||||||||||||||
self.__pubsubhubs[session_handler] = psh | ||||||||||||||
return psh | ||||||||||||||
|
||||||||||||||
async def start(self): | ||||||||||||||
""" | ||||||||||||||
Background task evicting expired app data. Must be called at FastAPI application startup. | ||||||||||||||
|
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.
suggestion: Consider using an asyncio.Lock instead of threading.Lock
Since the rest of the code appears to be using asyncio constructs, it might be more consistent and potentially safer to use an asyncio.Lock instead of threading.Lock.