Go ahead to the las edit please
I'm facing some problems to make this code run correctly. It should be easy but there is something I do not manage correctly with "if name = 'main'"
I have 2 files, test1 and test2 and it works as simple as:
TEST1
import test2
test2.call(0,7)
TEST2
import multiprocessing as mp
in1 = 0
in2 = 0
def cube(x):
return x ** 3
def call(data1, data2):
global in1
global in2
in1 = data1
in2 = data2
if __name__ == '__main__':
pool = mp.Pool(processes=7)
results = [pool.apply_async(cube, args=(x,)) for x in range(in1, in2)]
output = [p.get() for p in results]
print(output)
I cannot make run the multiprocess because of the "__ main __" condition but if I change the condition to "test1", nothing happens and with "test2" as condition I receive a infinite loop with an error:
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes 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 an executable.
How should I proceed?
More:
Hello,
I just read In the documentation: "Calling freeze_support() has no effect when invoked on any operating system other than Windows. In addition, if the module is being run normally by the Python interpreter on Windows (the program has not been frozen), then freeze_support() has no effect."
Is is happening to my script? I always get the infinite loop in Pycharm using Python interpreter. How could I test it?
New Try
If I execute this script directly, it works and I get my calcs.
def cube(x):
calc = x ** 3
print(calc)
return calc
if __name__ == '__main__':
print('hola')
x = 2
y = 4
p1 = mp.Process(target=cube, args=(x,))
p2 = mp.Process(target=cube, args=(y,))
p1.start()
p2.start()
p1.join()
p2.join()
But when I execute it from another file and I change the condition __ main __ for "test2" (name of the own execution) I get the error showed above. What is so wrong trying to call a multiprocess from another file?