SoFunction
Updated on 2024-11-19

The role of join in python multithreading

I. Preface

When practicing python multiprocess syntax, the understanding of join is not very thorough, this article through code practice to deepen the understanding of join().

multiprocessing is a cross-platform version of the multiprocessing module provided by python. multiprocessing can make full use of multiple cores to improve the efficiency of the program. multiprocessing supports sub-processes, communication and sharing of data, and performs different forms of synchronization, providing components such as Process, Queue, Pipe The first step in the process is to use a processor to synchronize the process with the other processes. However, today's focus on understanding join. subsequent articles will gradually learn to introduce other components or features.

II Hands-on

The join() method can block the main process at the current location, bringing the process executing join() to an end before continuing with the main process code logic.

# encoding: utf-8
"""
author: yangyi@
time: 2019/7/30 11:20 AM
func:
"""

from multiprocessing import Process
import os
import time

def now():
  return str(('%Y-%m-%d %H:%M:%S', ()))


def func_1(name):
  print(now() + ' Run child process %s (%s)...' % (name, ()))
  (4)
  print(now() + ' Stop child process %s (%s)...\n' % (name, ()))


def func_2(name):
  print(now() + ' Run child process %s (%s)...' % (name, ()))
  (8)
  print(now() + ' hello world!')
  print(now() + ' Stop child process %s (%s)...\n' % (name, ()))


if __name__ == '__main__':
  print ('Parent process %s.' % ())
  p1 = Process(target=func_1, args=('func_1',))
  p2 = Process(target=func_2, args=('func_2',))
  print now() + ' Process start.'
  ()
  ()
  ()
  ()
  print now() + ' Process end .'

output result

The results show that

Process end . is printed after func1 and func2 have ended.

2.2 Removing the join() function

if __name__ == '__main__':
  print ('Parent process %s.' % ())
  p1 = Process(target=func_1, args=('func_1',))
  p2 = Process(target=func_2, args=('func_2',))
  print now() + ' Process start.'
  ()
  ()
  print now() + ' Process end .'

The results are as follows.

2.3 Remove join() from func_2

if __name__ == '__main__':
  print ('Parent process %s.' % ())
  p1 = Process(target=func_1, args=('func_1',))
  p2 = Process(target=func_2, args=('func_2',))
  print now() + ' Process start.'
  ()
  ()
  () ### After p1 has finished executing . The main process ends without waiting for p2 to execute.
  print now() + ' Process end .'

The results are as follows.

The result shows that the main thread "Process end" outputs after func_1 finishes executing without waiting for func_2 to finish executing.

2.4 Summary

When utilizing multithreading, it is common to have the child thread call start() before calling join(), allowing the master process to wait for the child process to finish before continuing with the subsequent logic.

topic of discussion

Would it be possible to call start() for each child process and then just call join() similar to.

()()()()

The above is the role of join() in python multithreading in detail, more information about python multithreading join() please pay attention to my other related articles!