3

This question did not help:

Please note I added below line in my program and that did not help to resolve the problem as explained below on Ubuntu platform.

multiprocessing.freeze_support()

Failed to create nested child process when python exe built using auto-py-to-exe on Ubuntu.

Below Python program works ok when I run it using python3 command on Ubuntu platform.

import multiprocessing as mp

def P3_sm():
    print("P3 created.")
    print("P3 destroyed.")

def P2_sm():
    print("P2 created.")
    P3 = mp.Process(target=P3_sm)
    P3.start()
    P3.join()
    print("P2 destroyed.")

if __name__ == '__main__':
    mp.freeze_support() # ADDING THIS LINE DID NOT HELP!
    print("P1 created.")
    P2 = mp.Process(target=P2_sm)
    P2.start()
    P2.join()
    print("P1 destroyed.")

I built an exe using auto-py-to-exe tool which formed below command and generated exe. pyinstaller --nonconfirm --onefile --windowed "/home/max/test/main.py"

When I run this exe it only prints below 2 lines and never terminates the program. Any help is highly appreciated!

P1 created.
P2 created.

I tried solution as per below link but that did not work for me.

PyInstaller and multiprocessing

When I run the exe file on Ubuntu platform I'm expecting it to print below 6 lines and terminate the program as it does when I run the main.py file using python3 command.

P1 created.
P2 created.
P3 created.
P3 destroyed.
P2 destroyed.
P1 destroyed.
Max
  • 31
  • 3
  • 1
    You should edit the previous question to explain why the duplicate is inappropriate. – Barmar May 03 '23 at 17:22
  • 1
    Previous question is 7 years old and posted by a different person, also the error seems different at first glance – mousetail May 03 '23 at 17:23
  • You probably need to run `mp.freeze_support()` in every process. Not just the first – mousetail May 03 '23 at 17:24
  • I tried adding mp.freeze_support() at all places just before creating a new process even that did not help! Thanks for the hint though. – Max May 03 '23 at 17:26
  • Hi Does .exe run in linux? How? Anyway, [what do you think of this](https://stackoverflow.com/questions/65636830/error-when-running-a-exe-file-using-python-multiprocessing) – bonCodigo May 03 '23 at 17:26
  • 1
    You make the file as executable using "sudo chmod 0777 main.exe" and then you can run it using command "sudo ./main.exe". It is not necessary to have the file name as .exe you can run a file without having any extension name as long as you make it as an executable file using chmod command as shown above. – Max May 03 '23 at 17:33
  • @bonCodigo The link you gave me is for Windows platform. The behavior and the error output is different on Debian system. However, as I explained in my question the line "multiprocessing.freeze_support() did not help for my program. – Max May 03 '23 at 17:41

1 Answers1

0

You might want to get rid of the --windowed bit and add --console, --windowed is for generating GUIs, not console programs. I'm not sure if that's what's causing the weird behavior, but it's the only thing I see that's weird.

Brock Brown
  • 956
  • 5
  • 11
  • I tried --console option instead of --windowed that did not help either. Thank you for the suggestion though. – Max May 03 '23 at 17:38
  • @Max No problem. Are you using Wine to run this .exe? If you're trying to get this to run on a Windows machine, have you tried it on Windows yet? Wine is notorious for being a little buggy. It's possible that if you use a `threading` implementation instead of `multiprocessing` that it will behave better also. – Brock Brown May 03 '23 at 17:48
  • My target platform is Ubuntu. The code excerpt is from a huge existing project and I am not planning on changing the way it is implemented. It has to use processes and not threads and it must run on Ubuntu and not on Windows. Thanks! – Max May 03 '23 at 17:52
  • @Max `.exe` files are kind of a Windows thing (https://en.wikipedia.org/wiki/.exe). I would advise against distributing your program as an `.exe` if Ubuntu is your target. Is there a specific reason you would like it implemented this way? – Brock Brown May 03 '23 at 17:55
  • When I run auto-py-to-exe it generates a file called "main" I can avoid manually naming it as "main.exe" but how does that going to help? I will see the same problem even with file "main". The target PC runs Ubuntu OS so I have to get this one working on Ubuntu. – Max May 03 '23 at 18:01
  • @Max Hold on, from what I just read, you're compiling the `.exe`, renaming it to `main`, and then attempting to run it? If this is the case, you should just do `python file_name.py`. Python is an interpreted language, not a compiled one. https://www.freecodecamp.org/news/compiled-versus-interpreted-languages/ – Brock Brown May 03 '23 at 18:09
  • No compiling is done. Not sure if you read my question in full. Using Python interpreter file "main.py" works ok on Ubuntu platform. I use auto-py-to-exe to package it to run it on PC running Ubuntu OS. – Max May 03 '23 at 18:14
  • @Max I did read your question in full. PyInstaller is an .exe compiler. From https://pyinstaller.org/en/stable/usage.html: *"PyInstaller bundles the dynamic libraries of tcl and tk into the application at compile time."* Why not just run it like you did before? This is how it's normally done. Maybe you want to create a "main" file that contains `python name_of_your_program.py`, then `chmod a+x main` to make it executable? – Brock Brown May 03 '23 at 18:28
  • The reason I want to package my program is I do not want send my source code to customers so I am using auto-py-to-exe to bundle it as one file. I tested bundling other python files they work great when my Python program don't create 2 or more nested child processes. – Max May 03 '23 at 18:37
  • 1
    @Max Oh, why didn't you just say so? Use an obfuscator. https://pypi.org/project/pyminifier/ – Brock Brown May 03 '23 at 18:48
  • Interesting... never heard about it before and I am not sure how it works. Looks like it changes contents of .py files to a non-latin charaters or some sort of encoded characters that human can't read the source code file contents? – Max May 03 '23 at 18:58
  • 1
    @Max Yup! Makes it incredibly inconvenient anyway. If you use that `--non-latin` option it really cranks up the unreadability, it will scatter the obfuscated code to the left and right sides of the screen in western text editors. – Brock Brown May 03 '23 at 19:01
  • 1
    Thank you! I will keep this as my second option if I can't find any resolution with using the auto-py-to-exe tool. By the way, is there a command to reverse or unobfuscate a file to get back the original .py file by feeding the obfuscated file using pyminifier tool? – Max May 03 '23 at 19:07
  • @Max No prob. As far as I'm aware, I don't think you can deobfuscate it. *Maybe* GPT-4 could figure out how to deobfuscate it to some extent, I've never asked it though. – Brock Brown May 03 '23 at 19:09