Process pool in python's process

  • Create a process pool
  • Specifying the number of process pools means that there are at most the specified number of columns in the process pool.
pool = multiprocessing.Pool(3)
  • Use synchronization to execute tasks. Processes in the process pool need to wait for other processes to complete before executing the specified task.
pool.apply(copy_work)
  • Asynchronous execution, the processes in the process pool are executed together and will not wait for the execution of other processes
pool.apply_async(copy_work)

Tip: The main process will not wait for the process pool to complete the task execution before the program exits

Tip: The process pool no longer receives other tasks that need to be executed

pool.close()
  • Wait for the process pool to complete the task execution before the program exits
pool.join()

queue in the process pool

queue = multiprocessing.Manager().Queue()

# 创建进程池
pool = multiprocessing.Pool(2)
# 让进程池执行写入数据的任务
# pool.apply(write_data, (queue,))
# # 让进程池执行读取数据的任务
# pool.apply(read_data, (queue,))
result = pool.apply_async(write_data, (queue,))

Wait for the asynchronous task execution to complete before the code continues to execute

  1. Solution 2
result.wait()

1. Use time delay

# time.sleep(1.1)
pool.apply_async(read_data, (queue,))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325406185&siteId=291194637