[Python] Python concurrent.futures trying to import functions

Discussão em 'Python' iniciado por Stack, Janeiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    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()

    Continue reading...

Compartilhe esta Página