#๐Ÿ”’ App freezes and crashes when I attempt to run a flask-socketio server. (please help)

15 messages ยท Page 1 of 1 (latest)

dry ivy
#

I'm making a desktop app for myself and a couple buddies to assist in a dnd campaign we're trying to run figured it would be a good way to learn some new skills, but thats unimportant.

Long story short, the application opens fine but when I click the 'Host' button in the app, it freezes, yet the server still starts, or at the bare minimum, I can see its getting requests. I originally attempted using asyncio to try and mitigate these problems since i have a hunch the problem is being caused because starting the server is not apart of the main loop required to keep the gui up but tkinter doesnt like asyncio and so I turned to threading which i've had some luck on but it still isnt working. Any input would be appreciated; sorry if theres no reply, its late and im tired.

P.S: Sorry for the messy code and documentation, organization was never my strong suit.

https://paste.pythondiscord.com/PSWA

craggy orbitBOT
#

@dry ivy

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hallow hill
#

Yeah, sio.run(..) starts the mainloop for the server. This is called inside tkinter's mainloop, which is now blocked, so the server continues to run but your GUI will freeze

#

I'm not too familiar with neither tkinter nor socketio/flask, but from a brief search it seems it should be fine to run the flask app in a separate thread

#

I did notice that you did something like that with the "Connect" button, which calls Thread_Serv, which does start a server in a separate thread

#

The Host button does not do that though, it calls the blocking function directly

#

I also read that you need to disable the reloader flask uses, as it requires flask to run on the main thread: sio.run(..., use_reloader=False)

dry ivy
#

So if Iโ€™m understanding correctly I should start sio.run() in a thread to not block the tkinter main loop. Sound about right?

As for the connect button, I think I did that on accident and I meant to swap it to the other button, though I could be wrong. Edit: button was my screw up upon a second look

And I will make sure to try the reloader.

hallow hill
#

Yeah. Have the function that starts the server start it in a new thread. This could be as simple as calling .run as part of threading.Thread(), for example: ```py
server_thread = threading.Thread(target=lambda: app.run(app, host='0.0.0.0', port=8080, use_reloader=False))
server_thread.start()

#

getting communication working between tkinter and flask might be a bit of a challenge, since you can't just call flask methods from tkinter, or tkinter methods from flask.

#

you'd have to do something like put commands/events in a queue and have flask and tkinter consume events from it to communicate

#

but that is mostly a guess on my part, this is an area I'm not familiar with

dry ivy
#

Alright, sweet, thanks for the help! Iโ€™m gonna head to bed now but I got confidence this will work, and at the very least, I got places to start. Again, thanks for all the help!

craggy orbitBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.