In our previous tutorial, we learned about Python CSV Example. In this tutorial we are going to learn Python Multiprocessing with examples.

在上一教程中,我们了解了Python CSV Example 。 在本教程中,我们将通过示例学习Python多重处理。

Python多处理 (Python Multiprocessing)

Parallel processing is getting more attention nowadays. If you still don’t know about the parallel processing, learn from wikipedia.

如今,并行处理越来越受到关注。 如果您仍然不了解并行处理,请向Wikipedia学习。

As CPU manufacturers start adding more and more cores to their processors, creating parallel code is a great way to improve performance. Python introduced multiprocessing module to let us write parallel code.

随着CPU制造商开始向其处理器中添加越来越多的内核,创建并行代码是提高性能的好方法。 Python引入了多处理模块,让我们可以编写并行代码。

To understand the main motivation of this module, we have to know some basics about parallel programming. After reading this article, we hope that, you would be able to gather some knowledge on this topic.

要了解此模块的主要动机,我们必须了解有关并行编程的一些基础知识。 阅读本文后,我们希望您能够收集有关此主题的一些知识。

Python多处理进程,队列和锁 (Python Multiprocessing Process, Queue and Locks)

There are plenty of classes in python multiprocessing module for building a parallel program. Among them, three basic classes are Process, Queue and Lock. These classes will help you to build a parallel program.

python多重处理模块中有许多类可用于构建并行程序。 其中三个基本类是ProcessQueueLock 。 这些类将帮助您构建并行程序。

But before describing about those, let us initiate this topic with simple code. To make a parallel program useful, you have to know how many cores are there in you pc. Python Multiprocessing module enables you to know that. The following simple code will print the number of cores in your pc.

但是在描述这些内容之前,让我们用简单的代码启动该主题。 为了使并行程序有用,您必须知道您的PC中有多少个内核。 Python Multiprocessing模块使您知道这一点。 以下简单代码将打印您PC中的内核数。

import multiprocessingprint("Number of cpu : ", multiprocessing.cpu_count())

The following output may vary for your pc. For me, number of cores is 8.

以下输出可能因您的电脑而异。 对我来说,核心数是8。

Python多处理Process类 (Python multiprocessing Process class)

Python multiprocessing Process class is an abstraction that sets up another Python process, provides it to run code and a way for the parent application to control execution.

Python多重处理Process类是一种抽象,它建立了另一个Python进程,为它提供了运行代码的方式,并为父应用程序控制执行提供了一种方法。

There are two important functions that belongs to the Process class – start() and join() function.

有两个重要的函数属于Process类start()join()函数。

At first, we need to write a function, that will be run by the process. Then, we need to instantiate a process object.

首先,我们需要编写一个将由进程运行的函数。 然后,我们需要实例化一个流程对象。

If we create a process object, nothing will happen until we tell it to start processing via start() function. Then, the process will run and return its result. After that we tell the process to complete via join() function.

如果我们创建一个流程对象,那么直到我们告诉它通过start()函数开始处理之前,什么都不会发生。 然后,该过程将运行并返回其结果。 之后,我们通过join()函数告诉该过程完成。

Without join() function call, process will remain idle and won’t terminate.

没有join()函数调用,进程将保持空闲状态并且不会终止。

So if you create many processes and don’t terminate them, you may face scarcity of resources. Then you may need to kill them manually.

因此,如果您创建许多进程而没有终止它们,则可能会面临资源短缺。 然后,您可能需要手动杀死它们。

One important thing is, if you want to pass any argument through the process you need to use args keyword argument. The following code will be helpful to understand the usage of Process class.

一件重要的事情是,如果要在过程中传递任何参数,则需要使用args关键字参数。 以下代码将有助于理解Process类的用法。

from multiprocessing import Processdef print_func(continent='Asia'):print('The name of continent is : ', continent)if __name__ == "__main__":  # confirms that the code is under main functionnames = ['America', 'Europe', 'Africa']procs = []proc = Process(target=print_func)  # instantiating without any argumentprocs.append(proc)proc.start()# instantiating process with argumentsfor name in names:# print(name)proc = Process(target=print_func, args=(name,))procs.append(proc)proc.start()# complete the processesfor proc in procs:proc.join()

The output of the following code will be:

以下代码的输出将是:

Python多处理Queue类 (Python multiprocessing Queue class)

You have basic knowledge about computer data-structure, you probably know about Queue.

您具有有关计算机数据结构的基本知识,您可能也了解Queue。

Python Multiprocessing modules provides Queue class that is exactly a First-In-First-Out data structure. They can store any pickle Python object (though simple ones are best) and are extremely useful for sharing data between processes.

Python多重处理模块提供的Queue类完全是先进先出数据结构。 它们可以存储任何pickle的Python对象(虽然最好是简单的对象),并且对于在进程之间共享数据非常有用。

Queues are specially useful when passed as a parameter to a Process’ target function to enable the Process to consume data. By using put() function we can insert data to then queue and using get() we can get items from queues. See the following code for a quick example.

当队列作为参数传递给流程的目标函数以使流程能够使用数据时,队列特别有用。 通过使用put()函数,我们可以将数据插入然后排队,而使用get()我们可以从队列中获取项目。 请参见以下代码以获取快速示例。

from multiprocessing import Queuecolors = ['red', 'green', 'blue', 'black']
cnt = 1
# instantiating a queue object
queue = Queue()
print('pushing items to queue:')
for color in colors:print('item no: ', cnt, ' ', color)queue.put(color)cnt += 1print('\npopping items from queue:')
cnt = 0
while not queue.empty():print('item no: ', cnt, ' ', queue.get())cnt += 1

Python多处理锁类 (Python multiprocessing Lock Class)

The task of Lock class is quite simple. It allows code to claim lock so that no other process can execute the similar code until the lock has be released. So the task of Lock class is mainly two. One is to claim lock and other is to release the lock. To claim lock the, acquire() function is used and to release lock release() function is used.

Lock类的任务非常简单。 它允许代码声明锁,以便在释放锁之前没有其他进程可以执行类似的代码。 因此,Lock类的任务主要是两个。 一种是要求锁定,另一种是释放锁定。 要声明锁定,使用acquire()函数,并释放锁定release()函数。

Python多处理示例 (Python multiprocessing example)

In this Python multiprocessing example, we will merge all our knowledge together.

在这个Python多处理示例中,我们将所有知识融合在一起。

Suppose we have some tasks to accomplish. To get that task done, we will use several processes. So, we will maintain two queue. One will contain the tasks and the other will contain the log of completed task.

假设我们要完成一些任务。 为了完成该任务,我们将使用几个过程。 因此,我们将维持两个队列。 一个将包含任务,另一个将包含已完成任务的日志。

Then we instantiate the processes to complete the task. Note that the python Queue class is already synchronized. That means, we don’t need to use the Lock class to block multiple process to access the same queue object. That’s why, we don’t need to use Lock class in this case.

然后,我们实例化流程以完成任务。 请注意,python Queue类已经同步。 这意味着,我们不需要使用Lock类来阻止多个进程访问同一队列对象。 这就是为什么在这种情况下我们不需要使用Lock类。

Below is the implementation where we are adding tasks to the queue, then creating processes and starting them, then using join() to complete the processes. Finally we are printing the log from the second queue.

下面是将任务添加到队列中,然后创建进程并启动它们,然后使用join()完成过程的实现。 最后,我们从第二个队列中打印日志。

from multiprocessing import Lock, Process, Queue, current_process
import time
import queue # imported for using queue.Empty exceptiondef do_job(tasks_to_accomplish, tasks_that_are_done):while True:try:'''try to get task from the queue. get_nowait() function will raise queue.Empty exception if the queue is empty. queue(False) function would do the same task also.'''task = tasks_to_accomplish.get_nowait()except queue.Empty:breakelse:'''if no exception has been raised, add the task completion message to task_that_are_done queue'''print(task)tasks_that_are_done.put(task + ' is done by ' + current_process().name)time.sleep(.5)return Truedef main():number_of_task = 10number_of_processes = 4tasks_to_accomplish = Queue()tasks_that_are_done = Queue()processes = []for i in range(number_of_task):tasks_to_accomplish.put("Task no " + str(i))# creating processesfor w in range(number_of_processes):p = Process(target=do_job, args=(tasks_to_accomplish, tasks_that_are_done))processes.append(p)p.start()# completing processfor p in processes:p.join()# print the outputwhile not tasks_that_are_done.empty():print(tasks_that_are_done.get())return Trueif __name__ == '__main__':main()

Depending on the number of task, the code will take some time to show you the output. The output of the following code will vary from time to time.

根据任务的数量,代码将需要一些时间来显示输出。 以下代码的输出将不时变化。

Python多处理池 (Python multiprocessing Pool)

Python multiprocessing Pool can be used for parallel execution of a function across multiple input values, distributing the input data across processes (data parallelism). Below is a simple Python multiprocessing Pool example.

Python多处理池可用于跨多个输入值并行执行功能,从而跨进程分配输入数据(数据并行性)。 下面是一个简单的Python多处理池示例。

from multiprocessing import Poolimport timework = (["A", 5], ["B", 2], ["C", 1], ["D", 3])def work_log(work_data):print(" Process %s waiting %s seconds" % (work_data[0], work_data[1]))time.sleep(int(work_data[1]))print(" Process %s Finished." % work_data[0])def pool_handler():p = Pool(2)p.map(work_log, work)if __name__ == '__main__':pool_handler()

Below image shows the output of the above program. Notice that pool size is 2, so two executions of work_log function is happening in parallel. When one of the function processing finishes, it picks the next argument and so on.

下图显示了以上程序的输出。 请注意,池大小为2,因此work_log函数的两个执行并行发生。 当函数处理之一完成时,它将选择下一个参数,依此类推。

So, that’s all for python multiprocessing module.

因此,这就是python多处理模块的全部内容。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/15631/python-multiprocessing-example

Python多处理示例相关推荐

  1. python 加速器 numba 示例

    python 加速器 numba 示例 官方文档 http://numba.pydata.org/numba-doc/latest/user/index.html 示例代码 # pip install ...

  2. python 装饰器示例

    python 装饰器示例 import timedef decorator(func): # 传函数def wrapper(*args, **kwargs): # 传参数(也可以传固定参数)start ...

  3. python软件代码示例-python 示例代码1

    第一章 python基础一 ​在此不再赘述为什么学习python这门编程,网上搜索一箩筐.我在此仅说一句python的好,用了你就会爱上它. 本python示例代码1000+带你由浅入深的了解pyth ...

  4. python爬虫入门实例-终于领会python爬虫入门示例

    随着人工智能 大数据的火热 Python成为了广大科学家和普通大众的学习语言.在学习Python的过程中 有很多人感到迷茫 不知道自己该从什么地方入手,今天我们就来说一些新手该如何学习Python编程 ...

  5. python入门代码示例-总算知道python入门代码示例

    Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.作为今年来越来越流行的语言,我们该如何学习或者转行学习Python呢,这里小迹为大家介绍如何入门学习Python.以下是小编为你整理 ...

  6. python怎么反转单链表_单链表反转python实现代码示例

    单链表的反转可以使用循环,也可以使用递归的方式 1.循环反转单链表 循环的方法中,使用pre指向前一个结点,cur指向当前结点,每次把cur->next指向pre即可. 代码: class Li ...

  7. Python面向对象基础示例_创建对象

    Python面向对象基础示例_创建对象 python是面向对象的语言,你可以很轻松的创建一个类和对象.本书代码源自<<Python 3 Object-Oriented Programmin ...

  8. Python描述性统计示例

    Python描述性统计示例 1 声明 本文的数据来自网络,部分代码也有所参照,这里做了注释和延伸,旨在技术交流,如有冒犯之处请联系博主及时处理. 2 描述性统计分析简介 描述性统计分析是指运用制表和分 ...

  9. python常用语法和示例_使用Python中的示例进行输入和输出操作

    python常用语法和示例 A Program needs to interact with the user to accomplish the desired task; this is done ...

  10. python常用语法和示例_C语言切换案例教程,语法,示例和规则

    python常用语法和示例 使用默认情况下的决策 (Decision making using switch-case-default) Many times in our daily lives, ...

最新文章

  1. WEBGIS体系和OGC标准
  2. css知识笔记(四)——代码简写、颜色值、长度值
  3. 不仅是人类的shooow
  4. java常用快捷键 智能提示 及快捷键冲突
  5. 软件测试项目实战(web+app+h5+小程序)
  6. 手机上php文件用什么打开方式,php是什么文件格式 php文件打开方法【图文】
  7. 如何通过蒲公英批量获取iPhone设备的udid
  8. 如何查找电脑的MAC地址?(上)
  9. mybatis简单查询
  10. 渗透测试之信息收集总结
  11. #####好好好好######Neo4j 第三篇:Cypher查询入门
  12. 谈谈CANopen协议的机制
  13. 63. 请简述构造方法和析构方法的作用?
  14. 【转】推送消息推送机制
  15. windows 系统netstat -ano查看机器端口占用情况
  16. 华为机试HJ69:矩阵乘法
  17. 面试mysql之SQL优化总结一:索引的使用
  18. 【资源】OpenCV3编程入门_毛星云
  19. 大数据分析-第二章 大数据平台
  20. 【2020年第二届“网鼎杯”网络安全大赛 青龙组】Web AreUSerialz

热门文章

  1. Hibernate持久化对象状态
  2. jquery一些 事件的用法
  3. JQuery操作cookie插件
  4. [转载] Python print输出不换行没空格
  5. [转载] [转载] python反三角函数arctan_Python numpy.arctan() 使用实例
  6. full stack front end
  7. Kali下的内网劫持(一)
  8. mvc报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容
  9. MyBatis基础入门--知识点总结
  10. 【学习OpenCV4】如何操作图像中的像素?