-
Notifications
You must be signed in to change notification settings - Fork 454
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
Multiprocessing not compatible with Flet #4283
Comments
I think the reason that Multiprocessing is exhibiting the same issue as subprocess, is the one that you have identified already - the usage of sys.executable, and what that actually is for a Flet application.
|
I suspect all applications that utilise multiprocessing will need the following line as the first line in the if name block.. but I'm unsure whether/if anything is required on the dev side
|
I am using the new dev build that you provided for subprocess, and can confirm that the sys.argv are coming through. It seems that multiprocessing is indeed passing arguments using sys.argv, and all of them seem to at least contain "multiprocessing" (so I use an IN to filter an IF). However, I have been unable to use this to get multiprocessing working in the way that I thought it should work. In the below code, I have moved the import of Flet to be conditional, but even so - when the button is clicked, the GUI appears to duplicate. Even if Multiprocessing was not getting what it needed, I would expect the behaviour in the below program to be null action. I suspect that multiprocessing may have a separate issue to the GUI duplication, as in the console.log file I note that it gives the following
But I am still puzzled why the GUI duplicates. import logging
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("flet_core")
logging.getLogger("flet")
import sys
import os
argv = str(sys.argv )
logging.debug(f"sys.argv: {argv}")
orig_argv = str(sys.orig_argv)
logging.debug(f"sys.orig_argv: {orig_argv}")
from multiprocessing import Queue
import numpy as np
from concurrent.futures import ProcessPoolExecutor, as_completed
import multiprocessing
def sort_sublist(sublist):
"""Sorts a sublist of numbers."""
return sorted(sublist)
def parallel_sort(queue_var):
num_elements = 10_000_000
data = np.random.rand(num_elements).tolist()
number_of_sorts = 20
"""Sorts a list of numbers in parallel."""
with ProcessPoolExecutor(max_workers=10) as executor:
futures = [executor.submit(sort_sublist, data) for _ in range(number_of_sorts)] # Sort the same list x times
for future in as_completed(futures):
queue_var.put(1)
def main(page):
import flet as ft
page.title = "Flet Background Task Example"
queue_var = Queue()
text = ft.Text("Init")
def update_text():
completed = 0
while True:
completed += queue_var.get()
text.value = completed
text.update()
def on_click(e):
page.run_thread(parallel_sort,queue_var)
update_text()
page.add(
ft.ElevatedButton("Start Loop", on_click=on_click),
)
page.add(
text
)
page.add(ft.Text(f"sys.argv: {str(sys.argv)}"))
if __name__ == "__main__":
multiprocessing.freeze_support()
if 'multiprocessing' in ' '.join(sys.argv):
argv = str(sys.argv )
logging.debug(f"Multiprocessing hit! sys.argv: {argv}")
pass
else:
argv = str(sys.argv )
logging.debug(f"LINE HIT! sys.argv: {argv}")
import flet as ft
ft.app(target=main) Build commands used:
|
I don't see
|
I get the same. The IF statement is intended to route any argument containing multiprocessing away from the launching the Flet app or any Flet imports. In the above, when using sys.argv, any time that "multiprocessing" is found in the args, it routes it away from Flet. Multiprocessing seems to pass further arguments after resource tracker, once it fires off the processes - but all of the arguments it passes have commonality, in that it passes "from multiprocessing". The below is an example of what I was intending. Whenever "Python" is found within the args (once all joined), we enter the IF. (ofc, using orig_argv instead - as by default we get an extra arg to demonstrate the join behaviour) import sys
orig_argv = str(sys.orig_argv)
print("The original argv: ",orig_argv)
orig_argv_joined = ' '.join(sys.orig_argv)
print("The joined orig_argv: ",orig_argv_joined)
if 'Python' in orig_argv_joined:
print('Python is in argv')
|
OK, I've played a bit, managed to fix some issues in Flet build template and got multiprocessing "partially" working. Here is my last example: import logging
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("flet_core")
logging.getLogger("flet")
import os
import sys
argv = str(sys.argv)
logging.debug(f"sys.argv: {argv}")
orig_argv = str(sys.orig_argv)
logging.debug(f"sys.orig_argv: {orig_argv}")
logging.debug(f"env vars: {os.environ}")
import multiprocessing
from concurrent.futures import ProcessPoolExecutor, as_completed
from multiprocessing import Queue
import numpy as np
def sort_sublist(sublist):
"""Sorts a sublist of numbers."""
return sorted(sublist)
def parallel_sort(queue_var):
num_elements = 10_000_000
data = np.random.rand(num_elements).tolist()
number_of_sorts = 20
"""Sorts a list of numbers in parallel."""
with ProcessPoolExecutor(max_workers=1) as executor:
futures = [
executor.submit(sort_sublist, data) for _ in range(number_of_sorts)
] # Sort the same list x times
for future in as_completed(futures):
queue_var.put(1)
def main(page):
import flet as ft
page.title = "Flet Background Task Example"
queue_var = Queue()
text = ft.Text("Init")
def update_text():
completed = 0
while True:
completed += queue_var.get()
text.value = completed
text.update()
def on_click(e):
page.run_thread(parallel_sort, queue_var)
update_text()
def window_event(e):
print(e)
if e.data == "close":
sys.exit(0)
page.window.destroy()
page.window.prevent_close = True
page.window.on_event = window_event
page.add(
ft.ElevatedButton("Start Loop", on_click=on_click),
)
page.add(
ft.ElevatedButton("Quit app", on_click=lambda _: sys.exit(0)),
)
page.add(text)
page.add(ft.Text(f"sys.argv: {str(sys.argv)}"))
page.add(ft.Text(os.getenv("FLET_HIDE_WINDOW_ON_START")))
if __name__ == "__main__":
multiprocessing.freeze_support()
c_arg = "-c"
c_arg_provided = False
if c_arg in sys.argv:
c_arg_idx = sys.argv.index(c_arg)
if c_arg_idx < len(sys.argv) - 1:
c_arg_provided = True
exec(sys.argv[c_arg_idx + 1])
if not c_arg_provided:
os.environ["FLET_HIDE_APP_ON_START"] = "true"
import flet as ft
ft.app(target=main) So, some improvements:
What I can't understand is why it's holding child processes hanging after However, after clicking "Start Loop" the 3rd process is started (I limited to 1 worker) and then neither closing window nor clicking "Quit app" don't terminate child processes. Looking at multiprocessing module sources I understand it communicates with child worker processes via pipes (so it passes pipe name/number in How to drop child processes? Should you call some dispose/cleanup/release login in your app? Hope that helps. |
I've tried the above code on both Windows and Mac, and when the app is built, it shows a blank screen on both (even after the initialisation). The below is my Windows command, in which I clear the cache (I like the proposed change btw), and then build it using the latest Flet.
requirements.txt
In the below code, I have only changed two lines for the purposes of debugging, and I am finding that the application, when run using flet run main.py, the application cannot be exited, in the same way that occurs in your above example. (it doesnt run using Flet build). import logging
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("flet_core")
logging.getLogger("flet")
import os
import sys
argv = str(sys.argv)
logging.debug(f"sys.argv: {argv}")
orig_argv = str(sys.orig_argv)
logging.debug(f"sys.orig_argv: {orig_argv}")
logging.debug(f"env vars: {os.environ}")
import multiprocessing
from concurrent.futures import ProcessPoolExecutor, as_completed
from multiprocessing import Queue
import numpy as np
#################CHANGE
import flet as ft
def sort_sublist(sublist):
"""Sorts a sublist of numbers."""
return sorted(sublist)
def parallel_sort(queue_var):
num_elements = 10_000_000
data = np.random.rand(num_elements).tolist()
number_of_sorts = 20
"""Sorts a list of numbers in parallel."""
with ProcessPoolExecutor(max_workers=1) as executor:
futures = [
executor.submit(sort_sublist, data) for _ in range(number_of_sorts)
] # Sort the same list x times
for future in as_completed(futures):
queue_var.put(1)
def main(page):
import flet as ft
page.title = "Flet Background Task Example"
queue_var = Queue()
text = ft.Text("Init")
def update_text():
completed = 0
while True:
completed += queue_var.get()
text.value = completed
text.update()
def on_click(e):
page.run_thread(parallel_sort, queue_var)
update_text()
def window_event(e):
print(e)
if e.data == "close":
sys.exit(0)
page.window.destroy()
page.window.prevent_close = True
page.window.on_event = window_event
page.add(
ft.ElevatedButton("Start Loop", on_click=on_click),
)
page.add(
ft.ElevatedButton("Quit app", on_click=lambda _: sys.exit(0)),
)
page.add(text)
page.add(ft.Text(f"sys.argv: {str(sys.argv)}"))
page.add(ft.Text(os.getenv("FLET_HIDE_WINDOW_ON_START")))
if __name__ == "__main__":
#################CHANGE
ft.app(target=main)
# multiprocessing.freeze_support()
# c_arg = "-c"
# c_arg_provided = False
# if c_arg in sys.argv:
# c_arg_idx = sys.argv.index(c_arg)
# if c_arg_idx < len(sys.argv) - 1:
# c_arg_provided = True
# exec(sys.argv[c_arg_idx + 1])
# if not c_arg_provided:
# os.environ["FLET_HIDE_APP_ON_START"] = "true"
# import flet as ft
# ft.app(target=main) Terminal output when clicking the "X" in the Windows native toolbar.
|
I am still encountering an issue when the application is built - I cant get it to launch the multiprocess workers. The above issue where the window would not launch has been resolved by one of the intermediate Flet versions though. Observed behavior: Using Flet Run:
Flet Build:
Workaround considered:
Question:
Thanks for the support on this - appreciate all the effort going into this on your side. import logging
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("flet_core").setLevel(logging.WARNING)
logging.getLogger("flet").setLevel(logging.WARNING)
import os
import sys
argv = str(sys.argv)
logging.debug(f"sys.argv: {argv}")
orig_argv = str(sys.orig_argv)
logging.debug(f"sys.orig_argv: {orig_argv}")
logging.debug(f"env vars: {os.environ}")
import multiprocessing
from concurrent.futures import ProcessPoolExecutor, as_completed
from multiprocessing import Queue
import numpy as np
import csv
import os
def sort_sublist(sublist, sort_number):
##### ! edit output folder ! ####
output_folder = "/Users/alexproctor/Documents/CSV_OUTPUT"
##### ! edit output folder ! ####
"""Sorts a sublist of numbers and saves it to a CSV file in the specified folder."""
sorted_sublist = sorted(sublist)
filename = os.path.join(output_folder, f"sorted_list_{sort_number}.csv")
with open(filename, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(sorted_sublist)
return filename
def parallel_sort(queue_var):
num_elements = 10_000_000
data = np.random.rand(num_elements).tolist()
number_of_sorts = 100
with ProcessPoolExecutor(max_workers=1) as executor:
futures = [
executor.submit(sort_sublist, data, i) for i in range(number_of_sorts)
]
for future in as_completed(futures):
filename = future.result()
print(f"Sorted list saved to {filename}")
queue_var.put(1)
def main(page):
import flet as ft
page.title = "Flet Background Task Example"
queue_var = Queue()
text = ft.Text("Init")
def update_text():
completed = 0
while True:
completed += queue_var.get()
text.value = completed
text.update()
def on_click(e):
page.run_thread(parallel_sort, queue_var)
update_text()
def window_event(e):
print(e)
if e.data == "close":
sys.exit(0)
page.window.destroy()
page.window.prevent_close = True
page.window.on_event = window_event
page.add(
ft.ElevatedButton("Start Loop", on_click=on_click),
)
page.add(
ft.ElevatedButton("Quit app", on_click=lambda _: sys.exit(0)),
)
page.add(text)
page.add(ft.Text(f"sys.argv: {str(sys.argv)}"))
page.add(ft.Text(os.getenv("FLET_HIDE_WINDOW_ON_START")))
if __name__ == "__main__":
multiprocessing.freeze_support()
logging.debug("sys.argv: ",sys.argv)
c_arg = "-c"
c_arg_provided = False
if c_arg in sys.argv:
c_arg_idx = sys.argv.index(c_arg)
if c_arg_idx < len(sys.argv) - 1:
c_arg_provided = True
exec(sys.argv[c_arg_idx + 1])
logging.debug("sys.argv[c_arg_idx + 1]: ",sys.argv[c_arg_idx + 1])
if not c_arg_provided:
os.environ["FLET_HIDE_APP_ON_START"] = "true"
import flet as ft
ft.app(target=main) |
Answering your question: the most of the work to support multiprocessing is done in flet build template project, not Flet package itself, so we could further look into that once 0.25 is released. It's independent work. |
Here is an example of the issue that I am finding with multiprocessing. The GUI effectively doubles when Multiprocessing is called. The reason for this is that Python's Multiprocessing calls sys.executable to obtain the Python interpreter to run the new processes/workers. (see comment)
When the Flet application is built, as discussed, sys.executable will by default point to the application executable. Hence, this behaviour will only be observed within applications that have been built using Flet Build, and not Flet Run.. further, PyInstaller seem to have fixed this - so it isn't present in Flet Pack.
Application:
Behaviour:
#main.py
#requirements.txt
Originally posted by @ap4499 in #4252 (comment)
The text was updated successfully, but these errors were encountered: