Hello! I decided at the weekend to understand the multiprocessing in python. Because multithreading is locked in GIL, there is a lot of information on the multiprocessing module for paralleling tasks on the Internet

Sketched a test program (os Windows):

import timeit from multiprocessing import Process import os a = timeit.default_timer() def writer(name): print ('start child process with id: ', os.getpid()) with open(name + "txt", 'w') as fout: for i in xrange(500000): print >> fout, name print(timeit.default_timer() - a) if __name__=='__main__': p = Process(target=writer, args=('mom',)) p1 = Process(target=writer, args=('olk',)) p.start() p1.start() print('Done ' + str(timeit.default_timer() - a)) 

According to my idea, the processes begin to run in parallel in the background, and the sign with Done should appear first. In principle, the way it happens, but with an unpredictable time:

 (u'start child process with id: ', 3492) Done 1.73671324643 0.852093865662 (u'start child process with id: ', 4560) 0.933829923571 

Where does 1.73671324643 sec come from, after all Done is executed before everyone else?

  • one
    which OS? The starting point for timer.default_timer what (process start?) 2- What is a equal in different processes? - jfs
  • OS Windows 7 32-bit. The starting point is a = timeit.default_timer () in the main process. Put it inside if, the result is the same. In theory, a gets time immediately at startup, just before the processes start. It seems that there is no need to transfer variables from the main process in a particular way? - Alexander
  • one
    a is not a reference point for timeit.default_timer. For example, the starting point for time.time is time.gmtime (0) (1970-01-01). The starting point for time.clock on Windows is the time (wall-clock) elapsed since the first call. What function does timeit.default_timer call on your version of Python? 2- it should be understood that a in one process and a in another process are different objects (it is likely that the values ​​are the same, but should be explicitly checked). Try to answer both of the remaining questions from my first comment. - jfs

0