Skip to content
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

Fix async pubsub #868

Merged
merged 1 commit into from
Jan 13, 2023
Merged

Fix async pubsub #868

merged 1 commit into from
Jan 13, 2023

Conversation

roniemartinez
Copy link
Contributor

Pubsub examples are failing when converted to async (pre-release).

For example, converting the code from https://flet.dev/docs/controls/page#send_allmessage to async

from dataclasses import dataclass
import flet as ft

@dataclass
class Message:
    user: str
    text: str


async def main(page: ft.Page):

    async def on_broadcast_message(message):
        await page.add_async(ft.Text(f"{message.user}: {message.text}"))

    await page.pubsub.subscribe_async(on_broadcast_message)

    async def on_send_click(e):
        await page.pubsub.send_all_async(Message("John", "Hello, all!"))

    await page.add_async(ft.ElevatedButton(text="Send message", on_click=on_send_click))


ft.app(target=main)

The above will throw the following error as soon as the button is clicked.

Traceback (most recent call last):
  File "/Users/roniemartinez/Library/Caches/pypoetry/virtualenvs/flet-test-ytD6Reby-py3.11/lib/python3.11/site-packages/flet/flet.py", line 345, in on_event
    await conn.sessions[e.sessionID].on_event_async(
  File "/Users/roniemartinez/Library/Caches/pypoetry/virtualenvs/flet-test-ytD6Reby-py3.11/lib/python3.11/site-packages/flet_core/page.py", line 459, in on_event_async
    await handler(ce)
  File "/Users/roniemartinez/Projects/flet-test/example.py", line 18, in on_send_click
    await page.pubsub.send_all_async(Message("John", "Hello, all!"))
  File "/Users/roniemartinez/Library/Caches/pypoetry/virtualenvs/flet-test-ytD6Reby-py3.11/lib/python3.11/site-packages/flet/pubsub.py", line 189, in send_all_async
    await self.__pubsub.send_all_async(message)
  File "/Users/roniemartinez/Library/Caches/pypoetry/virtualenvs/flet-test-ytD6Reby-py3.11/lib/python3.11/site-packages/flet/pubsub.py", line 31, in send_all_async
    await self.__send_async(handler, [message])
  File "/Users/roniemartinez/Library/Caches/pypoetry/virtualenvs/flet-test-ytD6Reby-py3.11/lib/python3.11/site-packages/flet/pubsub.py", line 177, in __send_async
    asyncio.create_task(handler(**args))
                        ^^^^^^^^^^^^^^^
TypeError: __main__.main.<locals>.on_broadcast_message() argument after ** must be a mapping, not list

@CLAassistant
Copy link

CLAassistant commented Jan 12, 2023

CLA assistant check
All committers have signed the CLA.

@roniemartinez
Copy link
Contributor Author

Related to my comment in #128 (comment)

@FeodorFitsner
Copy link
Contributor

Thank you! I'm going to merge it as soon as PR build is finished.

@FeodorFitsner FeodorFitsner merged commit 8c06aae into flet-dev:main Jan 13, 2023
@roniemartinez roniemartinez deleted the fix-async-pubsub branch January 13, 2023 00:08
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.

3 participants