Reputation: 131
at first: I am trying to write GUI for controlling Flask simple server, so I can distribute my app to noobs (using PyInstaller)
I am using multiprocessing to launch Flask and then turn it off, but turning it off seems to be the problem. I wrote simple cli-controller as a proof-of-concept but it ignores even sys.exit()
Code:
from multiprocessing import Process
import sys
from myapp import app
def run():
app.run()
server = Process(target=run)
server.start()
while True:
x = raw_input("Input something:")
if x == "x":
server.terminate()
server.join(timeout=10)
print(server.exitcode)
print("end here")
break
print("All done!")
sys.exit(1)
But the result has blown my mind: (<Enter> means I pressed enter)
user@localhost:~$ ./run.py
Input something: * Running on http://127.0.0.1:5000/
* Restarting with reloader
Input something:x
None
end here
All done!
user@localhost:~$ <Enter> Traceback (most recent call last):
File "./run.py", line 42, in <module>
x = raw_input("Input something:")
EOFError
and Flask is still running...
server.join() after terminate() but Flask is running anyway
server=...
to break
in if __name__ == "__main__:"
with no resultWhat am I missing?
Upvotes: 0
Views: 1086
Reputation: 131
I still don't understand, why previous versions doesn't work, but I found this solution:
from Tkinter import *
from ttk import *
import multiprocessing
from myapp import app
def make_ui(server):
def terminate_server():
if server is not None and server.is_alive():
server.terminate()
main.destroy()
def toggle_server():
global server
if server.is_alive():
server.terminate()
server.join()
else:
if server.exitcode:
server = multiprocessing.Process(target=run_in_behind, name="server")
server.start()
main = Tk()
appname = Label(main, text="Sociometry v.0.0.1", anchor="center")
appname.grid(row=0, column=0, columnspan=2, pady=2)
main.mainloop()
def run_in_behind():
config = {
"HOST": "127.0.0.1"
}
app.config.from_object(config)
app.run()
if __name__ == "__main__":
server = multiprocessing.Process(target=run_in_behind, name="server")
make_ui(server)
Upvotes: 1