flet icon indicating copy to clipboard operation
flet copied to clipboard

Page.window_destroy very slow

Open quan787 opened this issue 4 years ago • 21 comments

import flet


def main(page: flet.Page):
    def quit_window(e):
        page.window_destroy()
    page.add(flet.TextButton(text='quit', on_click=quit_window))


flet.app(target=main)

It takes ~5 second to destory the window, frozen during that period. Is that possible to speed up that process?

quan787 avatar Sep 14 '22 21:09 quan787

What OS is that? Python version?

FeodorFitsner avatar Sep 14 '22 22:09 FeodorFitsner

Python 3.9.12 (tags/v3.9.12:b28265d, Mar 23 2022, 23:52:46) [MSC v.1929 64 bit (AMD64)] on win32

Windows 10 21H2

quan787 avatar Sep 14 '22 22:09 quan787

Could run your example with detailed logging enable and paste the results here:

import logging

import flet

logging.basicConfig(
    format="%(asctime)s %(levelname)-8s %(message)s",
    level=logging.DEBUG,
    datefmt="%Y-%m-%d %H:%M:%S",
)


def main(page: flet.Page):
    def quit_window(e):
        page.window_destroy()

    page.add(flet.TextButton(text="quit", on_click=quit_window))


flet.app(target=main)

FeodorFitsner avatar Sep 15 '22 17:09 FeodorFitsner

2022-09-16 01:52:38 INFO     Starting local Flet Server on port 10628...
2022-09-16 01:52:38 INFO     Attached process: True
2022-09-16 01:52:38 INFO     Flet Server found in: C:\Users\Z.Y.Li\AppData\Local\Programs\Python\Python39\lib\site-packages\flet\bin\fletd.exe
2022-09-16 01:52:38 INFO     Web renderer configured: canvaskit
2022-09-16 01:52:38 INFO     Route URL strategy configured: hash
2022-09-16 01:52:38 INFO     Connecting Flet Server at ws://127.0.0.1:10628/ws...
INFO[2022-09-16T01:52:38+08:00] Using in-memory cache
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

2022-09-16 01:52:38 DEBUG    Exited run_forever()
2022-09-16 01:52:38 INFO     Failed to connect: ws://127.0.0.1:10628/ws
2022-09-16 01:52:38 INFO     Reconnecting Flet Server in 0.1 seconds
2022-09-16 01:52:38 INFO     Connecting Flet Server at ws://127.0.0.1:10628/ws...
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: icons/icon-512.png       
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: icons/icon-maskable-512.png 
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: index.html
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: assets/FontManifest.json
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: assets/packages/window_manager/images/ic_chrome_close.png
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: assets/shaders/ink_sparkle.frag
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: icons/apple-touch-icon-192.png
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: assets/AssetManifest.json
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: assets/packages/window_manager/images/ic_chrome_minimize.png
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: assets/packages/window_manager/images/ic_chrome_unmaximize.png
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: main.dart.js
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: icons/icon-maskable-192.png
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: manifest.json
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: version.json
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: assets/NOTICES
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: assets/fonts/MaterialIcons-Regular.otf
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: assets/packages/window_manager/images/ic_chrome_maximize.png
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: favicon.png
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: flutter.js
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: flutter_service_worker.js
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: icons/icon-192.png
DEBU[2022-09-16T01:52:38+08:00] EmbedAssetsFS item: icons/loading-animation.png
DEBU[2022-09-16T01:52:38+08:00] Variable FLET_STATIC_ROOT_DIR with path to web static content is not set.
[GIN-debug] GET    /ws                       --> github.com/flet-dev/flet/server/server.Start.func1 (4 handlers)
[GIN-debug] GET    /api/                     --> github.com/flet-dev/flet/server/server.Start.func2 (4 handlers)
[GIN-debug] GET    /api/oauth/github         --> github.com/flet-dev/flet/server/server.githubAuthHandler (4 handlers)
[GIN-debug] GET    /api/oauth/azure          --> github.com/flet-dev/flet/server/server.azureAuthHandler (4 handlers)
[GIN-debug] GET    /api/oauth/google         --> github.com/flet-dev/flet/server/server.googleAuthHandler (4 handlers)
[GIN-debug] GET    /api/auth/signout         --> github.com/flet-dev/flet/server/server.signoutHandler (4 handlers)
[GIN-debug] PUT    /api/upload               --> github.com/flet-dev/flet/server/server.uploadFileAsStream (4 handlers)
INFO[2022-09-16T01:52:38+08:00] Starting server on :10628
INFO[2022-09-16T01:52:38+08:00] Starting background tasks...
INFO[2022-09-16T01:52:38+08:00] Start background task to cleanup expired data
2022-09-16 01:52:38 DEBUG    Exited run_forever()
2022-09-16 01:52:38 INFO     Reconnecting Flet Server in 0.1 seconds
2022-09-16 01:52:38 INFO     Connecting Flet Server at ws://127.0.0.1:10628/ws...
INFO[2022-09-16T01:52:38+08:00] New WebSocket client connected from 127.0.0.1: 83fadc86-2f28-4312-9589-d4bf4952aa5e 
[GIN] 2022/09/16 - 01:52:38 | 200 |       619.7µs |       127.0.0.1 | GET      "/ws"
2022-09-16 01:52:38 INFO     Successfully connected to ws://127.0.0.1:10628/ws
2022-09-16 01:52:38 DEBUG    _send_message_with_result: {"id":"37a42410f05f4cebb32b65826aa6ed0d","action":"registerHostClient","payload":{"hostClientID":null,"pageName":"","isApp":true,"update":false,"authToken":null,"permissions":null}}
DEBU[2022-09-16T01:52:38+08:00] Message from 83fadc86-2f28-4312-9589-d4bf4952aa5e: {"id":"37a42410f05f4cebb32b65826aa6ed0d","action":"registerHostClient","payload":{"hostClientID":null,"pageName":"","isApp":true,"update":false,"authToken":null,"permissions":null}}
INFO[2022-09-16T01:52:38+08:00] registerHostClient()
INFO[2022-09-16T01:52:38+08:00] Registering Host client: 83fadc86-2f28-4312-9589-d4bf4952aa5e
INFO[2022-09-16T01:52:38+08:00] Registering host client 83fadc86-2f28-4312-9589-d4bf4952aa5e to handle 'p/index' sessions
2022-09-16 01:52:38 DEBUG    _on_message: {"id":"37a42410f05f4cebb32b65826aa6ed0d","action":"","payload":{"hostClientID":"bLucu5VhYLY3Pp7v-FQUclQLQ9bfuHjyOMVqo_ar4w5CNfHHbcegY9Y-dhJCVI0AtsB5gg9IME0YZJZAgQAe6YofVzu4LEwUiig=","sessionID":"","pageName":"p/index","error":""}}      
2022-09-16 01:52:39 INFO     App URL: http://127.0.0.1:10628
2022-09-16 01:52:39 INFO     Connected to Flet app and handling user sessions...
2022-09-16 01:52:39 INFO     Starting Flet View app...
2022-09-16 01:52:39 INFO     Flet View found in: C:\Users\Z.Y.Li\AppData\Local\Programs\Python\Python39\lib\site-packages\flet\bin\flet\flet.exe
INFO[2022-09-16T01:52:39+08:00] New WebSocket client connected from 127.0.0.1: ad9d5918-db84-43a5-871e-13c6e4159d70 
[GIN] 2022/09/16 - 01:52:39 | 200 |      2.9216ms |       127.0.0.1 | GET      "/ws"
DEBU[2022-09-16T01:52:39+08:00] Message from ad9d5918-db84-43a5-871e-13c6e4159d70: {"action":"registerWebClient","payload":{"pageName":"","pageRoute":"/","pageWidth":"1265.6","pageHeight":"682.4","windowWidth":"1280.0","windowHeight":"720.0","windowTop":"9.6","windowLeft":"9.6","isPWA":"false","isWeb":"false","platform":"windows","sessionId":""}}
INFO[2022-09-16T01:52:39+08:00] registerWebClient()
DEBU[2022-09-16T01:52:39+08:00] Subscribe web client ad9d5918-db84-43a5-871e-13c6e4159d70 to 'p/index' page name
INFO[2022-09-16T01:52:39+08:00] Registering Web client: ad9d5918-db84-43a5-871e-13c6e4159d70
INFO[2022-09-16T01:52:39+08:00] Registering Web client ad9d5918-db84-43a5-871e-13c6e4159d70 to session p/index:c807e623-1ca2-43bb-98f2-52f1730887bb
DEBU[2022-09-16T01:52:39+08:00] New session c807e623-1ca2-43bb-98f2-52f1730887bb started for p/index page
2022-09-16 01:52:39 DEBUG    _on_message: {"id":"","action":"sessionCreated","payload":{"pageName":"p/index","sessionID":"c807e623-1ca2-43bb-98f2-52f1730887bb"}}
2022-09-16 01:52:39 DEBUG    _send_message_with_result: {"id":"23ea48d2bfb44a9e85d0befacf40e848","action":"pageCommandsBatchFromHost","payload":{"pageName":"p/index","sessionID":"c807e623-1ca2-43bb-98f2-52f1730887bb","commands":[{"n":"get","v":["page","route"]},{"n":"get","v":["page","pwa"]},{"n":"get","v":["page","web"]},{"n":"get","v":["page","platform"]},{"n":"get","v":["page","width"]},{"n":"get","v":["page","height"]},{"n":"get","v":["page","windowWidth"]},{"n":"get","v":["page","windowHeight"]},{"n":"get","v":["page","windowTop"]},{"n":"get","v":["page","windowLeft"]}]}}
DEBU[2022-09-16T01:52:39+08:00] Message from 83fadc86-2f28-4312-9589-d4bf4952aa5e: {"id":"23ea48d2bfb44a9e85d0befacf40e848","action":"pageCommandsBatchFromHost","payload":{"pageName":"p/index","sessionID":"c807e623-1ca2-43bb-98f2-52f1730887bb","commands":[{"n":"get","v":["page","route"]},{"n":"get","v":["page","pwa"]},{"n":"get","v":["page","web"]},{"n":"get","v":["page","platform"]},{"n":"get","v":["page","width"]},{"n":"get","v":["page","height"]},{"n":"get","v":["page","windowWidth"]},{"n":"get","v":["page","windowHeight"]},{"n":"get","v":["page","windowTop"]},{"n":"get","v":["page","windowLeft"]}]}}
DEBU[2022-09-16T01:52:39+08:00] Page commands batch from host client
DEBU[2022-09-16T01:52:39+08:00] Page commands batch response to 83fadc86-2f28-4312-9589-d4bf4952aa5e - &{[/ false false windows 1265.6 682.4 1280.0 720.0 9.6 9.6] }
2022-09-16 01:52:39 DEBUG    _on_message: {"id":"23ea48d2bfb44a9e85d0befacf40e848","action":"","payload":{"results":["/","false","false","windows","1265.6","682.4","1280.0","720.0","9.6","9.6"],"error":""}}
2022-09-16 01:52:39 INFO     Session started: c807e623-1ca2-43bb-98f2-52f1730887bb
2022-09-16 01:52:39 DEBUG    _send_message_with_result: {"id":"ca168a642f434f588e92bfde62444ec9","action":"pageCommandsBatchFromHost","payload":{"pageName":"p/index","sessionID":"c807e623-1ca2-43bb-98f2-52f1730887bb","commands":[{"n":"add","a":{"to":"page","at":"0"},"c":[{"v":["view"]},{"i":2,"v":["textbutton"],"a":{"text":"quit"}}]},{"n":"add","a":{"to":"page","at":"1"},"c":[{"v":["offstage"]},{"i":2,"v":["clipboard"],"a":{"value":"{\"ts\":\"1663264359.5004604\"}"}},{"i":2,"v":["clientstorage"]},{"i":2,"v":["launchurl"],"a":{"value":"{\"ts\":\"1663264359.5004604\"}"}}]}]}}
DEBU[2022-09-16T01:52:39+08:00] Message from 83fadc86-2f28-4312-9589-d4bf4952aa5e: {"id":"ca168a642f434f588e92bfde62444ec9","action":"pageCommandsBatchFromHost","payload":{"pageName":"p/index","sessionID":"c807e623-1ca2-43bb-98f2-52f1730887bb","commands":[{"n":"add","a":{"to":"page","at":"0"},"c":[{"v":["view"]},{"i":2,"v":["textbutton"],"a":{"text":"quit"}}]},{"n":"add","a":{"to":"page","at":"1"},"c":[{"v":["offstage"]},{"i":2,"v":["clipboard"],"a":{"value":"{\"ts\":\"1663264359.5004604\"}"}},{"i":2,"v":["clientstorage"]},{"i":2,"v":["launchurl"],"a":{"value":"{\"ts\":\"1663264359.5004604\"}"}}]}]}}
DEBU[2022-09-16T01:52:39+08:00] Page commands batch from host client
DEBU[2022-09-16T01:52:39+08:00] Page commands batch response to 83fadc86-2f28-4312-9589-d4bf4952aa5e - &{[_1 _2 _3 _4 _5 _6] }
2022-09-16 01:52:39 DEBUG    _on_message: {"id":"ca168a642f434f588e92bfde62444ec9","action":"","payload":{"results":["_1 _2","_3 _4 _5 _6"],"error":""}}
DEBU[2022-09-16T01:52:48+08:00] Expired entries: 0
DEBU[2022-09-16T01:52:48+08:00] cleanupPagesAndSessions()
DEBU[2022-09-16T01:52:48+08:00] cleanupExpiredClients()
DEBU[2022-09-16T01:52:48+08:00] cleanupExpiredPageNameRegistrations()

Roughly this is the point when I pressed the botton:

DEBU[2022-09-16T01:52:52+08:00] Message from ad9d5918-db84-43a5-871e-13c6e4159d70: {"action":"pageEventFromWeb","payload":{"eventTarget":"_2","eventName":"click","eventData":""}}
DEBU[2022-09-16T01:52:52+08:00] Page event from browser: {"eventTarget":"_2","eventName":"click","eventData":""} PageName: p/index SessionID: c807e623-1ca2-43bb-98f2-52f1730887bb
2022-09-16 01:52:52 DEBUG    _on_message: {"id":"","action":"pageEventToHost","payload":{"pageName":"p/index","sessionID":"c807e623-1ca2-43bb-98f2-52f1730887bb","eventTarget":"_2","eventName":"click","eventData":""}}
2022-09-16 01:52:52 INFO     page.on_event: _2 click
2022-09-16 01:52:52 DEBUG    _send_message_with_result: {"id":"effacb18d9c34ede8309f313b9a8619d","action":"pageCommandsBatchFromHost","payload":{"pageName":"p/index","sessionID":"c807e623-1ca2-43bb-98f2-52f1730887bb","commands":[{"n":"set","v":["page"],"a":{"windowdestroy":"true"}}]}}
DEBU[2022-09-16T01:52:52+08:00] Message from 83fadc86-2f28-4312-9589-d4bf4952aa5e: {"id":"effacb18d9c34ede8309f313b9a8619d","action":"pageCommandsBatchFromHost","payload":{"pageName":"p/index","sessionID":"c807e623-1ca2-43bb-98f2-52f1730887bb","commands":[{"n":"set","v":["page"],"a":{"windowdestroy":"true"}}]}}
DEBU[2022-09-16T01:52:52+08:00] Page commands batch from host client
DEBU[2022-09-16T01:52:52+08:00] Page commands batch response to 83fadc86-2f28-4312-9589-d4bf4952aa5e - &{[] }
2022-09-16 01:52:52 DEBUG    _on_message: {"id":"effacb18d9c34ede8309f313b9a8619d","action":"","payload":{"results":[],"error":""}}        
INFO[2022-09-16T01:52:57+08:00] Exiting WebSocket read loop
INFO[2022-09-16T01:52:57+08:00] Exiting WebSocket write loop
DEBU[2022-09-16T01:52:57+08:00] WebSocket client disconnected (normal closure=false) from 127.0.0.1: ad9d5918-db84-43a5-871e-13c6e4159d70  
INFO[2022-09-16T01:52:57+08:00] Unregistering Web client ad9d5918-db84-43a5-871e-13c6e4159d70 (1 sessions)
INFO[2022-09-16T01:52:57+08:00] Unregistering Web client ad9d5918-db84-43a5-871e-13c6e4159d70 from session p/index:c807e623-1ca2-43bb-98f2-52f1730887bb
DEBU[2022-09-16T01:52:57+08:00] Unsubscribe web client ad9d5918-db84-43a5-871e-13c6e4159d70 from 'p/index' page name
2022-09-16 01:52:57 DEBUG    _on_message: {"id":"","action":"pageEventToHost","payload":{"pageName":"p/index","sessionID":"c807e623-1ca2-43bb-98f2-52f1730887bb","eventTarget":"page","eventName":"disconnect","eventData":""}}
DEBU[2022-09-16T01:52:57+08:00] Exit subscribe(): ad9d5918-db84-43a5-871e-13c6e4159d70
DEBU[2022-09-16T01:52:57+08:00] Exit extendExpiration(): ad9d5918-db84-43a5-871e-13c6e4159d70
2022-09-16 01:52:57 INFO     page.on_event: page disconnect
2022-09-16 01:52:57 DEBUG    Closing connection...
INFO[2022-09-16T01:52:57+08:00] Exiting WebSocket read loop
DEBU[2022-09-16T01:52:57+08:00] WebSocket client disconnected (normal closure=true) from 127.0.0.1: 83fadc86-2f28-4312-9589-d4bf4952aa5e   
2022-09-16 01:52:57 DEBUG    Exited run_forever()
INFO[2022-09-16T01:52:57+08:00] Unregistering Host client 83fadc86-2f28-4312-9589-d4bf4952aa5e (0 sessions)
INFO[2022-09-16T01:52:57+08:00] Unregistering host client 83fadc86-2f28-4312-9589-d4bf4952aa5e from 'p/index' page
INFO[2022-09-16T01:52:57+08:00] Exiting WebSocket write loop
DEBU[2022-09-16T01:52:57+08:00] Delete expired page name: 83fadc86-2f28-4312-9589-d4bf4952aa5e
DEBU[2022-09-16T01:52:57+08:00] Exit subscribe(): 83fadc86-2f28-4312-9589-d4bf4952aa5e
DEBU[2022-09-16T01:52:57+08:00] Exit extendExpiration(): 83fadc86-2f28-4312-9589-d4bf4952aa5e
INFO[2022-09-16T01:52:57+08:00] Deleting page: p/index
PS C:\Users\Z.Y.Li\Documents\vscode\test_tray> INFO[2022-09-16T01:52:57+08:00] Parent process exited with code 0
INFO[2022-09-16T01:52:57+08:00] Shutting down server...
INFO[2022-09-16T01:52:57+08:00] Server exited

quan787 avatar Sep 15 '22 17:09 quan787

OK, what if instead page.window_destroy() you use sys.exit()?

FeodorFitsner avatar Sep 15 '22 20:09 FeodorFitsner

No, sys.exit() doesn't quit the script at all.

Also, the window may need to be reopened, by let's say a system tray icon. Now I'm using page.window_visible to hide the window instead of destroying it as a more proper way, but it hurts the exiting speed nevertheless.

So maybe you can add hiding to the beginning of page.window_destroy() , and let it quit quietly?

quan787 avatar Sep 15 '22 20:09 quan787

So maybe you can add hiding to the beginning of page.window_destroy() , and let it quit quietly?

Great idea. The User will not have to wait for some seconds before the window 'officially' closes. Some sort of illusion.

But what if, in the button's on_click event callback, you try to hide the window first...then destroy it. Have you tried?

ndonkoHenri avatar Sep 19 '22 20:09 ndonkoHenri

But what if, in the button's on_click event callback, you try to hide the window first...then destroy it. Have you tried?

Yes and it works. My initial concern is it may slow down things in a loop. Then I realized I shouldn't be coding that way, since only a single window is supported and I shouldn't create and destroy it many times.

quan787 avatar Sep 19 '22 20:09 quan787

How many times is the user allowed to destroy the window? Just once right¿

What do you mean by "many times" then please ?

ndonkoHenri avatar Sep 19 '22 21:09 ndonkoHenri

I was doing this. I have a system tray icon, when a user presses the icon, a window will pop up. when it get closed, it is destroyed. When the icon is clicked again, a new window will show again.

It's not the right way to do it, I have to admit.

quan787 avatar Sep 19 '22 21:09 quan787

But why do you have to destroy the window over an over?? Why not just hide and show it once more when the tray icon is pressed again ?

That could even help save the app's state. When I look at real world stuffs, that's how it is. What do you think ?

ndonkoHenri avatar Sep 19 '22 21:09 ndonkoHenri

But why do you have to destroy the window over an over?? Why not just hide and show it once more when the tray icon is pressed again ?

That could even help save the app's state. When I look at real world stuffs, that's how it is. What do you think ?

ndonkoHenri avatar Sep 19 '22 21:09 ndonkoHenri

You're totally right. I realized this about a day after this issue was raised. I'm closing this since it's not very relevant.

quan787 avatar Sep 19 '22 21:09 quan787

Hey @quan787 Is the destroy still slow(like back then) with the latest version?

ndonkoHenri avatar Jan 23 '23 18:01 ndonkoHenri

Yeah, I removed all throttling lately.

FeodorFitsner avatar Jan 23 '23 20:01 FeodorFitsner

Ah. So, it should be way more faster now? So?

ndonkoHenri avatar Jan 23 '23 21:01 ndonkoHenri

Yep. It was 300 ms throttling, now it's 0.

FeodorFitsner avatar Jan 23 '23 21:01 FeodorFitsner

Just to be curious, what caused that?

ndonkoHenri avatar Jan 23 '23 21:01 ndonkoHenri

For desktop apps "fletd" in Golang using webservices was replaced with a light-weight Fletd shim written in Python. There is a little latency right now and Python code is not "flooding" with a lot of window events when you move it, resize it, etc.

FeodorFitsner avatar Jan 23 '23 21:01 FeodorFitsner

Got it! Thanks.

ndonkoHenri avatar Jan 23 '23 21:01 ndonkoHenri