Detailed code for python multiprocess usage and thread pool usage

  • 2021-01-03 20:57:55
  • OfStack

Multi-process: Mainly runs multiprocessing module


import os,time
import sys
from multiprocessing import Process
class MyProcess(Process):
  """docstring for MyProcess"""
  def __init__(self, arg, callback):
    super(MyProcess, self).__init__()
    self.arg = arg
    self.callback = callback
  def run(self):
    self.callback(self.arg)
def test(arg):
  print(" The child process {} start >>> pid={}".format(arg,os.getpid()))
  for i in range(1,5):
    sys.stdout.write(" The child process {} In the operation of the {}\r".format(arg,i))
    sys.stdout.flush()
    time.sleep(1)
def main():
  print(" Main process start >>> pid={}".format(os.getpid()))
  myp=MyProcess(1,test)
  myp.start()
  myp2=MyProcess(2,test)
  myp2.start()
  myp.join()
  myp2.join()
  print(" Main process termination ")
if __name__ == '__main__':
  main()

Thread pool: mainly uses the future module! In the following example, the first thread pool is normal, the second thread pool is normal, and the third thread pool is running 2 threads, which will be queued


from concurrent.futures import ThreadPoolExecutor
import time
def sayhello(a):
  print("hello: "+a)
  time.sleep(2)
def main():
  seed=["a","b","c"]
  start1=time.time()
  for each in seed:
    sayhello(each)
  end1=time.time()
  print("time1: "+str(end1-start1))
  start2=time.time()
  with ThreadPoolExecutor(3) as executor:
    for each in seed:
      executor.submit(sayhello,each)
  end2=time.time()
  print("time2: "+str(end2-start2))
  start3=time.time()
  with ThreadPoolExecutor(2) as executor1:
    executor1.map(sayhello,seed)
  end3=time.time()
  print("time3: "+str(end3-start3))
if __name__ == '__main__':
  main()

conclusion


Related articles: