Skip to content

Multiprocessing problems with flet build windows executable #3547

Closed

Description

@xuying-92

Duplicate Check

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

Activity

  1. ndonkoHenri commented on Aug 17, 2026

    @ndonkoHenri
    Contributor

    This is now resolved in Flet 0.86.0 (#6662). Docs: https://flet.dev/docs/cookbook/multiprocessing
    Packaged desktop apps now detect Python multiprocessing child invocations before Flutter starts and run them using a headless embedded Python interpreter. This prevents worker processes from opening additional Flet windows or locking the unpacked application directory.

    Your example already seems to follow the main requirements: the worker is defined at module level and the Flet entry point is protected by if name == "main":. freeze_support() is no longer required, although leaving it there is harmless. Please upgrade to Flet 0.86.0 or newer and rebuild the Windows application. Let know if you face further issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions