Python Multiprocessing: The Complete Guide

本文翻译自: Python Multiprocessing: The Complete Guide

1.进程与线程
一个进程指向一个计算机程序。每一个进程事实上是执行Python指令的Python解释器的一个实例,所执行的Python指令比你写入Python程序中的级别略低。

进程:操作系统派生和控制的实体,它封装了正在执行的应用程序。进程有两个主要功能。第一个是作为应用程序的资源持有者,第二个是执行应用程序的指令。

线程存在于进程之内,代表了指令或代码被执行的行为。

一个进程将至少有一个线程,称为主线程。任何我们在进程之内额外创建的线程都将属于这个进程。

Python进程将在所有线程(非后台线程)终止后终止。

2.在一个进程之内运行函数
通过使用multiprocessing.Process类,Python函数可以在一个独立的进程中执行。

(1)如何在一个进程中运行一个函数
首先,我们创造一个multiprocessing.Process类的新实例,并在一个新的进程中通过"target"参数指出我们想要执行的函数。

# create a process
process = multiprocessing.Process(target=task)

在另一个进程中执行的函数可能有参数,在这种情况下,它们可以被指定为一个元组,并传递给multiprocessing的“args”参数。进程类构造函数或作为“kwargs”参数的字典。

# create a process
process = multiprocessing.Process(target=task, args=(arg1, arg2))

start()函数将会立刻返回,并且操作系统将会在一个独立的进程中尽可能快的执行函数:

# run the new process
process.start()

Python解释器的一个新实例将会被创建,并且新进程的一个新线程将会被创建以便执行我们的目标函数。

(2)在进程中运行一个函数的实例
写一个自定义函数:

# a custom function that blocks for a moment
def task():
    # block for a moment
    sleep(1)
    # display a message
    print('This is from another process')

start()函数不会block,这意味这它会立即返回。
完整示例:

# SuperFastPython.com
# example of running a function in another process
from time import sleep
from multiprocessing import Process
 
# a custom function that blocks for a moment
def task():
    # block for a moment
    sleep(1)
    # display a message
    print('This is from another process')
 
# entry point
if __name__ == '__main__':
    # create a process
    process = Process(target=task)
    # run the process
    process.start()
    # wait for the process to finish
    print('Waiting for the process...')
    process.join()

(3) 在进程中运行一个函数且带有参数的实例

# SuperFastPython.com
# example of running a function with arguments in another process
from time import sleep
from multiprocessing import Process
 
# a custom function that blocks for a moment
def task(sleep_time, message):
    # block for a moment
    sleep(sleep_time)
    # display a message
    print(message)
 
# entry point
if __name__ == '__main__':
    # create a process
    process = Process(target=task, args=(1.5, 'New message from another process'))
    # run the process
    process.start()
    # wait for the process to finish
    print('Waiting for the process...')
    process.join()

3.扩展Process类

(1) 如何扩展Process类
例如:

# custom process class
class CustomProcess(multiprocessing.Process):
    # ...

multiprocessing.Process类中的run()函数必须被重写以包含你希望在另一个进程中执行的代码。
比如:

# override the run function
def run(self):
    # ...

您还可以在类中定义其他函数,以拆分可能需要在另一个进程中完成的工作。最后,属性还可以用于存储在另一个进程中执行的任何计算或IO的结果,这些计算或IO之后可能需要检索。

(2) 扩展Process类的示例
代码:

# SuperFastPython.com
# example of extending the Process class
from time import sleep
from multiprocessing import Process
 
# custom process class
class CustomProcess(Process):
    # override the run function
    def run(self):
        # block for a moment
        sleep(1)
        # display a message
        print('This is coming from another process')
 
# entry point
if __name__ == '__main__':
    # create the process
    process = CustomProcess()
    # start the process
    process.start()
    # wait for the process to finish
    print('Waiting for the process to finish')
    process.join()

(3) 扩展Process类和返回值的实例
通过multiprocessing.Value类和multiprocessing.Array类,实例变量属性可以在进程之间被分享。这些类显式定义旨在以进程安全的方式在进程之间共享的数据属性。

共享变量意味着在一个进程中所做的更改总是被传播,并使其可用于其他流程。


暂时到这里

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值