flet icon indicating copy to clipboard operation
flet copied to clipboard

Multiprocessing problems with flet build windows executable

Open xuying-92 opened this issue 7 months ago • 0 comments

Duplicate Check

  • [X] I have searched the opened issues and there are no duplicates

Describe the bug

Hello Flet community,

I am using multiprocessing in my app, but after build the executable using flet build windows, and run the resulting executable, it shows the following error with 2 flet windows.

PathAccessException: Deletion failed, path = 'C:\Users\XUYI27~1\AppData\Local\Temp\multi-1.0.0-1\app' (OS Error: The process cannot access the file because it is being used by another process. , errno = 32)

Code

Below is a demo I wrote to demonstrate this problem. My actual app also uses Process, Queue and Event from multiprocessing.

import queue
import random
import time
from multiprocessing import Queue, Event, Process, freeze_support

import flet as ft

STOP_EVENT = Event()
RESULT_QUEUE = Queue()


def gen_rand(q: Queue, stop_event: Event):
    stop_gen = False
    while not stop_gen:
        if stop_event.is_set():
            stop_gen = True
        random_number = random.randint(0, 100)
        q.put(random_number)
        time.sleep(1)


def main(page: ft.Page):
    def on_window_event(e):
        if e.data == 'close':
            STOP_EVENT.set()
            while not RESULT_QUEUE.empty():
                RESULT_QUEUE.get()
            RESULT_QUEUE.close()
            process_gen.join(5)
            page.window.destroy()

    page.window.prevent_close = True
    page.window.on_event = on_window_event

    process_gen = Process(target=gen_rand, args=(RESULT_QUEUE, STOP_EVENT))
    process_gen.start()

    while not STOP_EVENT.is_set():
        try:
            rand_number = RESULT_QUEUE.get(timeout=5)
            page.add(ft.Text(f'Generated Number: {rand_number}'))
        except queue.Empty:
            pass


if __name__ == '__main__':
    freeze_support()
    ft.app(main)

To reproduce

  1. build the executable with flet build windows
  2. run the resulting exe

Expected behavior

A Test control with random number added to the app window every second, the same behavior as running the python script using python main.py or flet run

python_run_window

python_run_details

Screenshots

flet_build

flet_build_details

Operating System

Windows

Operating system details

Windows 10

Flet version

0.23.1

Regression

I'm not sure / I don't know

Suggestions

No response

Additional details

No response

xuying-92 avatar Jun 26 '24 08:06 xuying-92