1

I am trying to use one of the GGS functions in Python 2.7 (https://github.com/cvxgrp/GGS, the function that I am trying to use is located inside ggs.py and it is called GGSCrossVal, row 72) but Python shown me this error:

Attempt to start a new process before the current process has finished its bootstrapping phase. This probably means that you are on Windows and you have forgotten to use the proper idiom in the main module: if name == 'main': freeze_support() ... The "freeze_support()" line can be omitted if the program is not going to be frozen to produce a Windows executable.

Where do I have to put this command? .

I have generated a data matrix of 2x3740, data taken from a bivariate distribution with a certain mean and a certain covariance matrix (actually I have generated them in MATLAB and imported in Python) and called the function in this way:

trainTestResults = GGSCrossVal(data, 25, [10, 1, 0.1, 0.01, 0.001, 0.0001], [], False)

Could someone please help me? Thank you

  • `freeze_support` is not the issue here. please protect execution with `if __name__ == "__main__":` so none of your code is actually called when the file is imported as a module – Aaron Mar 18 '21 at 16:44
  • your code as posted does nothing but import stuff, and define a few functions. please show where you actually call the code. – Aaron Mar 18 '21 at 16:47
  • This code is far from "minimal" in the [MCVE](https://stackoverflow.com/help/minimal-reproducible-example). Please work towards the smallest example you can generate using this `GGSCrossVal` function which generates your error. – Aaron Mar 18 '21 at 16:52
  • Hi @Aaron, thank you for your answer! Actually is is not my code, I have found it in github and it is related to an article, which I have to analyze. I just know the basics in Python: where should I put the `` if __name__ == "__main__": ``? I am trying to use the GGSCrossVal in this way: I have generated a data matrix of 2x3740 (actually imported from MATLAB) and called the function in this way: trainTestResults = GGSCrossVal(data, 25, [10, 1, 0.1, 0.01, 0.001, 0.0001], [], False) – Marcello Marra Mar 18 '21 at 17:05
  • Please post your code by [editing your post](https://stackoverflow.com/posts/66681956/edit) The github link is sufficient for the library – Aaron Mar 18 '21 at 17:06
  • @Aaron, I have edited the post, thank you for your feedback :) – Marcello Marra Mar 18 '21 at 17:15
  • I've posted a somewhat short and sweet answer, but [This](https://stackoverflow.com/a/419185/3220135) answer is much more comprehensive – Aaron Mar 18 '21 at 17:28

1 Answers1

0

In windows and by default in macos, the way a new process is "spawn"ed basically amounts to "start a new python process, import all the same modules, import the 'main' file as a library, then use pickle to exchange which function to call and what the arguments are". The alternative to this on *nix systems is "fork", where the process memory is copied and the new process starts from the same point.

The important implication here is that when using "spawn" the "main" file you're running must not spawn more child threads when it is imported. If it did, the first children would spawn grandchildren when they import __main__, which would then spawn great-grandchildren when they import __main__, and so-on creating infintely recursive child processes. This is obviously a problem, so python raises an error if you attempt to create new processes in a child process during this import phase.

The solution to avoid this problem is to prevent any which spawns child processes from executing outside of the main process (this is also useful for things like running tests on a library when it's run as a main process rather than imported as a library).

if __name__ == "__main__":
    trainTestResults = GGSCrossVal(data, 25, [10, 1, 0.1, 0.01, 0.001, 0.0001], [], False)
Aaron
  • 10,133
  • 1
  • 24
  • 40