1

So I got 2 .py files and am trying to import the test function from the first to the secon one. But every time I try that I just get a "BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending." Error. I have no idea what Im messing up help is very much appreciated

parallel.py:

import time
from concurrent import futures


def test(t):
    time.sleep(t)
    print("Ich habe {} Sekunden gewartet. Zeit {:.0f}".format(t, time.time()))



def main():
    print("Startzeit:                         {:.0f}".format(time.time()))
    start = time.perf_counter()
    with futures.ThreadPoolExecutor(max_workers=3) as ex:
        ex.submit(test, 9)
        ex.submit(test, 4)
        ex.submit(test, 5)
        ex.submit(test, 6)

        print("Alle Aufgaben gestartet.")

    print("Alle Aufgaben erledigt.")

    finish = time.perf_counter()

    print("Fertig in ",round(finish-start,2)," seconds(s)")


 
if __name__ == "__main__":
    main()
    

parallel2.py:

import parallel
import time
import concurrent.futures

# =============================================================================
# def test(t):
#     time.sleep(t)
#     return ("Ich habe {} Sekunden gewartet. Zeit {:.0f}".format(t, time.time()))
# =============================================================================

def main():  
    print("Startzeit:                         {:.0f}".format(time.time()))

    start = time.perf_counter()
    
    with concurrent.futures.ProcessPoolExecutor() as executor:
        f1 = executor.submit(parallel.test, 9)
        f2 = executor.submit(parallel.test, 5)
        f3 = executor.submit(parallel.test, 4)
        f4 = executor.submit(parallel.test, 6)
        print(f1.result())
        print(f2.result())
        print(f3.result())
        print(f4.result())
 
    finish = time.perf_counter()

    print("Fertig in ",round(finish-start,2)," seconds(s)")    
    
if __name__ =="__main__":
    main()
user3666197
  • 1
  • 6
  • 50
  • 92
BrauseMann
  • 11
  • 1

1 Answers1

0

Test that solution:

Remove a condition if __name__ == "__main__" from parallel.py.

You put the condition in both scripts: if __name__ == "__main__" to execute the main function.

When doing this your script checks if it is the main module, and executes the function only if the return is true.

When you import another script, your module is no longer "__main__", so the return does not satisfy the condition imposed for the function to run. .