Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get mpi4py processes to finish printing before executing time.sleep()?

When I run the code below using mpiexec -n 5 python mpiTest.py, I expect every process to print its message immediately, then sleep for the specified amount of time. Instead, it executes as if I put the print command AFTER the sleep command. Why does this happen and how can I get it to behave as expected?

Adding a MPI.COMM_WORLD.Barrier() between the print and sleep commands does NOT help.

I'm using MS-MPI on win10.

from mpi4py import MPI
import random
import time

def delayed():
    random.seed()
    sek = random.randint( 1, 5 )
    print( "Delaying for ", sek, " seconds." )
    time.sleep( sek )
    return

delayed()
like image 629
endlesse Avatar asked Sep 15 '25 08:09

endlesse


1 Answers

Adding sys.stdout.flush() after the print call did the trick, thanks ever so much! =)

like image 187
endlesse Avatar answered Sep 17 '25 21:09

endlesse