Only one process prints in unix, multiprocessing python -


i have script i'm loading file takes while because there quite data read , prevent user terminating process want show kind of loading indication. thought opportunity learn how use multiprocessing module wrote example test module:

import time, multiprocessing  def progress():     delay = 0.5     while true:         print "loading.",         time.sleep(delay)         print "\b.",         time.sleep(delay)         print "\b.",         time.sleep(delay)         print "\r                        \r",      return  def loader(filename, con):     # dummy loader     time.sleep(5)     con.send(filename)     con.close()     return  if __name__ == "__main__":     parrent_con, child_con = multiprocessing.pipe()     filename = "main.key"      p1 = multiprocessing.process(target=progress)     p2 = multiprocessing.process(target=loader, args=(filename, child_con))      p1.start()     p2.start()      data = parrent_con.recv()     p1.terminate()     print "\n", data 

it works expect when run in windows cmd, prints "loading" , sequentially adds dots until loader complete. in unix need work don't output progress function, process p1.

just mark , dacav suggested, buffering problem. here possible solutions:

  • using python -u run script

    python -u unbuffer stdout , stderr. easiest solution if it's acceptable you.

  • using sys.stdout.flush

    sys.stdout.flush flush stdout buffer.

    delay = 0.5 while true:     print("loading."),     sys.stdout.flush()     time.sleep(delay)     print("\b."),     sys.stdout.flush()     time.sleep(delay)     print("\b."),     sys.stdout.flush()     time.sleep(delay)     print("\r                        \r"),     sys.stdout.flush() return 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -