使用消息传递接口在 Python 中实现多处理 [关闭]

Posted

技术标签:

【中文标题】使用消息传递接口在 Python 中实现多处理 [关闭]【英文标题】:Implement multiprocessing in Python with a message passing interface [closed] 【发布时间】:2021-03-12 04:49:55 【问题描述】:

我正在尝试将一些 javascript 代码转换为 Python,但是 JavaScript 以异步方式运行代码并且具有简单的事件发射器侦听器函数来在不同实例之间进行通信。在 Python 中有没有类似的方法可以做到这一点?

我尝试过使用 Process(来自多处理),但在任何地方都找不到简单的消息传递实现

【问题讨论】:

【参考方案1】:

这样的事情怎么样:

import time
import multiprocessing


def do_some_work(inputs):

    # Time consuming task
    time.sleep(0.1)

    # Some output
    return f"some response for input inputs"


if __name__ == "__main__":

    # Create a pool of 4 workers (can be more of course)
    with multiprocessing.Pool(4) as pool:

        # Assign a list of work to the pool of workers and get the results
        result = pool.map(do_some_work, [f"input_i" for i in range(100)])

    # Show the results
    print(result)

输出:

['some response for input input_0', 'some response for input input_1', 'some response for input input_2', 'some response for input input_3', 'some response for input input_4', 'some response for input input_5', 'some response for input input_6', 'some response for input input_7', 'some response for input input_8', 'some response for input input_9', 'some response for input input_10', 'some response for input input_11', 'some response for input input_12', 'some response for input input_13', 'some response for input input_14', 'some response for input input_15', 'some response for input input_16', 'some response for input input_17', 'some response for input input_18', 'some response for input input_19', 'some response for input input_20', 'some response for input input_21', 'some response for input input_22', 'some response for input input_23', 'some response for input input_24', 'some response for input input_25', 'some response for input input_26', 'some response for input input_27', 'some response for input input_28', 'some response for input input_29', 'some response for input input_30', 'some response for input input_31', 'some response for input input_32', 'some response for input input_33', 'some response for input input_34', 'some response for input input_35', 'some response for input input_36', 'some response for input input_37', 'some response for input input_38', 'some response for input input_39', 'some response for input input_40', 'some response for input input_41', 'some response for input input_42', 'some response for input input_43', 'some response for input input_44', 'some response for input input_45', 'some response for input input_46', 'some response for input input_47', 'some response for input input_48', 'some response for input input_49', 'some response for input input_50', 'some response for input input_51', 'some response for input input_52', 'some response for input input_53', 'some response for input input_54', 'some response for input input_55', 'some response for input input_56', 'some response for input input_57', 'some response for input input_58', 'some response for input input_59', 'some response for input input_60', 'some response for input input_61', 'some response for input input_62', 'some response for input input_63', 'some response for input input_64', 'some response for input input_65', 'some response for input input_66', 'some response for input input_67', 'some response for input input_68', 'some response for input input_69', 'some response for input input_70', 'some response for input input_71', 'some response for input input_72', 'some response for input input_73', 'some response for input input_74', 'some response for input input_75', 'some response for input input_76', 'some response for input input_77', 'some response for input input_78', 'some response for input input_79', 'some response for input input_80', 'some response for input input_81', 'some response for input input_82', 'some response for input input_83', 'some response for input input_84', 'some response for input input_85', 'some response for input input_86', 'some response for input input_87', 'some response for input input_88', 'some response for input input_89', 'some response for input input_90', 'some response for input input_91', 'some response for input input_92', 'some response for input input_93', 'some response for input input_94', 'some response for input input_95', 'some response for input input_96', 'some response for input input_97', 'some response for input input_98', 'some response for input input_99']

【讨论】:

以上是关于使用消息传递接口在 Python 中实现多处理 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

JAVA中实现多线程的四种方式

在python中实现多分支选择结构的较好方法是

2在NET中实现多线程

怎么在基于对话框的MFC程序中实现多线程?

Java中实现多线程的两种方式之间的区别

Java中实现多线程的两种方式