title: Multiprocessing.Process running entire script instead of just target function in python
body: <p>I have a simple example script that uses multiprocessing to execute a very simple function and return the runtimes for all parts of the process. The script is fully reproducible and looks as such:</p>
  <pre><code>import time
  start_time = time.perf_counter()
  
  import multiprocessing
  print(f'Libraries loaded: {round(time.perf_counter()-start_time,2)} sec')
  start_time = time.perf_counter()
  
  
  def test():
      print('Sleeping 1 sec')
      time.sleep(1)
      print('Done Sleeping')
  
  print(f'Functions loaded: {round(time.perf_counter()-start_time,2)} sec')
  start_time = time.perf_counter()
  
  
  if __name__ == '__main__':
      p1 = multiprocessing.Process(target=test)
      p2 = multiprocessing.Process(target=test)
      p1.start()
      p2.start()
      p1.join()
      p2.join()
  
      finish = time.perf_counter()
      print(f'Multiprocessing finished in {round(finish - start_time, 2)} sec')
  </code></pre>
  <p>The output of the script looks as such:</p>
  <pre><code>Libraries loaded: 0.03 sec
  Functions loaded: 0.0 sec
  Libraries loaded: 0.0 sec
  Functions loaded: 0.0 sec
  Sleeping 1 sec
  Libraries loaded: 0.0 sec
  Functions loaded: 0.0 sec
  Sleeping 1 sec
  Done SleepingDone Sleeping
  
  Multiprocessing finished in 1.12 sec
  
  Process finished with exit code 0
  </code></pre>
  <p>As you can see, whilst the multiprocesses are running in parallel, they run the entire script each time as opposed to just executing the target function <code>test</code>. The script is therefore being run twice completely unnecessarily and I don't understand why.</p>
  <p>Could someone explain this to me please?</p>
  <p>Thanks</p>