question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RFP: Async support in Flet Python client

See original GitHub issue

Discussed in https://github.com/flet-dev/flet/discussions/72

Problem

It is currently impossible to call async method from Flet event handlers. They are synchronous functions run in separate threads. This limitation requires to use bulky constructs like:

search_data  = new_event_loop().run_until_complete(get_weather(search_value))

where get_weather(search_value) is async function.

Solution

As in other languages like C# a “proper” async must be implemented from ground up - it’s impossible/ineffective to partially support calling async methods from sync ones. The entire program must be running in an event loop (in Python terms).

Flet Python client must be re-implemented to use asyncio library.

Sample code

Flet client will be providing both sync and async methods, for example async version of “Hello, world” might look like:

import asyncio
import flet as ft

async def main(page: ft.Page):

    async def btn_click(e):
        var result = await some_call_to_rest_api()
        page.controls.append(ft.Text(result))
        await page.update_async()

    await page.add_async(
        ft.Text("Hello!"),
        ft.ElevatedButton("Call async method", on_click=btn_click)
    )

ft.app(target=main)

All async methods will have _async suffix.

WebSockets client will be implemented with websockets library based on asyncio.

Resources:

Flet API

Sync is going to be default style of writing Flet apps. Flet API that needs to be available in both sync and async variants:

  • Connection class - have a base Connection class and two implementations: SyncConnection and AsyncConnection:
    • def connect()
    • async def connect_async()
    • def send_command(self, session_id: str, command: Command)
    • async def send_command_async(self, session_id: str, command: Command)
    • def send_commands(self, session_id: str, commands: List[Command])
    • async def send_commands_async(self, session_id: str, commands: List[Command])
    • def close(self)
    • on_event - could be either Callable or Coroutine
    • on_session_created - could be either Callable or Coroutine
    • ReconnectingWebSocket must be created inside SyncConnection.
    • Can we use send_commands only in all cases?
  • Page class:
    • TBD

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
FeodorFitsnercommented, Dec 8, 2022

We go ahead with Async support in Flet: https://flet.dev/blog/flet-mobile-update#asyncio-support

0reactions
TutorExiliuscommented, Dec 9, 2022

How wonderful, thanks! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

RFP: Async support in Flet Python client - PullAnswer
Anyone wanting to call into async functions from flet code could take advantage of the minimal unsync package, where a simple usage example...
Read more >
Blog | Flet
Async API support with async WebSockets library. Works with the same Fletd in Go. Fletd server ("stub") in Python to use with a...
Read more >
Appendix A. A Short History of Async Support in Python
The goal of this appendix is to describe a little of the history behind async programming in Python, and the point I want...
Read more >
While loop stuck at await asyncio.sleep(1) permanetly within ...
I am trying to make an asynchronous block of code to work within a flet module based app. The program starts just fine,...
Read more >
xiG - River Thames Conditions - Environment Agency - GOV.UK
Vatel paris contact, Flet poprzeczny sklep, Hog sticker rocket league, ... Simple server client python, Criticize unfairly thesaurus, Lohagarh forts ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found