关键词:
【中文标题】使用消息传递接口在 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']
【讨论】:
我可以使用优化实验在 Anylogic 中实现多目标优化问题吗?
】我可以使用优化实验在Anylogic中实现多目标优化问题吗?【英文标题】:CanIimplementamultiobjectiveoptimizationprobleminAnylogicusingoptimizationexperiment?【发布时间】:2021-02-1517:06:14【问题描述】:我正在尝试在Anylogic中使用基于Anylogic代理... 查看详情
怎么在基于对话框的mfc程序中实现多线程?
...望高手前辈指点! 参考技术A1.因为你是MFC程序,建议使用AfxBeginThread()来创建新线程。AfxBeginThread()的内部会做一些相应的初始化,再调用CreateThread()API。线程处理函数也就是一个普通的函数,但是其声明形式有规定的,如... 查看详情
在python中实现多分支选择结构的较好方法是
参考技术A(A.ifB.if-elseC.if-elif-else)。C8.下列语句执行后的输出是(if2:)。CD.if嵌套)。DB.notx)。DB.x-6>5B.2<xandx<=10D... 查看详情
使用 MySql 在 Java 中实现 Google Cloud 消息传递
】使用MySql在Java中实现GoogleCloud消息传递【英文标题】:GoogleCloudmessagingimplementationinJavausingMySql【发布时间】:2015-03-3008:08:12【问题描述】:我是一名在androidstudio中学习编写android代码的mac上的java开发人员。我被要求将谷歌云消... 查看详情
使用主数据库在 Spring MVC 应用程序中实现多租户?
】使用主数据库在SpringMVC应用程序中实现多租户?【英文标题】:ImplementingmultitenancyinSpringMVCapplicationwithamasterdatabase?【发布时间】:2018-12-0305:39:41【问题描述】:我正在尝试使用基于此代码源demo的SpringMVC、Springsecurity、Hibernate和... 查看详情
java中实现多线程继承thread类与实现runnable接口的区别
...运行的逻辑放在其中 2. 通过实现Runnable接口,实例化Thread类 在实际应用中,我们经常用到多线程,如车站的售票系统,车站的各个售票窗口相当于各个线程。当我们做这个系统的时候 查看详情
如何使用 Visual Studio for Mac 在 Xamarin.Forms 中实现多目标?
】如何使用VisualStudioforMac在Xamarin.Forms中实现多目标?【英文标题】:HowtomultitargetinXamarin.FormswithVisualStudioforMac?【发布时间】:2019-07-2323:37:14【问题描述】:我开始创建一个.NETStandardLibrary并打算创建一个NuGet-但后来发现我还需要... 查看详情
如何在 C 中实现多分支树结构
...试做一棵多叶树。我正在尝试将C#trie实现转换为C,以便使用CUDA在GPU上运行它。但我一直坚持这一点。你能帮我吗?我的节点实现如下:structNode2char*Key;char*ConsAlterKey;ch 查看详情
我应该如何在角度材料中实现多项选择选项?
...经检查了文档和演示,但是唉!!我还没有找到任何关于使用angular-material实现多选选项(如select2)的参考。谁能告诉我如何使它工作?【问题讨论】:稍微补充一下问题:从0. 查看详情
如何在 Firebase 身份验证中实现多用户帐户登录和切换?
...户视图上滑动以切换到另一个帐户。我在我的应用程序中使用Firebase身份验证。这样的多登录账 查看详情
系统运维系列之java中实现多线程的方式补充
...运维系列之java中实现多线程的方式上篇博客提到多线程使用的4种方式,分别是:实现方式1:继承Thread类,重写该类的run方法;实现方式2:实现Runnable接口,并重写该接口的run()方法;实现方式3ÿ... 查看详情
mybatis中实现多表查询
...询语句,在业务(Service)把查询的两表结果合并 1.2 使用AutoMapping特性,在实现两表联合查询时通过别名完成映射 1.3 使用MyBatis<resultMap>属性进行实现2、多表查询时,类中包含另一个类的对象的分类 2.1单个对... 查看详情
java示例代码_在Java中实现多线程池
java示例代码_在Java中实现多线程池 查看详情
在 MFC 中实现多线程以更新外部函数可访问的内部字典
...ctions【发布时间】:2011-12-2017:54:15【问题描述】:我正在使用C++和MFC创建一个应用程序,该应用程序将实时从Bloomberg中提取信息。BloombergAPI有一个订阅类,可以“订 查看详情
如何在angular js中实现多路由
】如何在angularjs中实现多路由【英文标题】:Howtoachievemultipleroutinginangularjs【发布时间】:2018-11-2223:10:07【问题描述】:我在AngularJS中练习routing。到目前为止,我已经研究了2页路由,但现在我想实现3页路由。(function()\'usestrict\';... 查看详情
如何在 laravel 护照中实现多身份验证
】如何在laravel护照中实现多身份验证【英文标题】:howtoimplementmultiauthinlaravelpassport【发布时间】:2018-09-0120:51:57【问题描述】:我有两个用户admin/user我想验证这两个用户的api,它适用于一个用户,但不适用于管理员看看我尝试... 查看详情
如何在 PostgreSQL 中实现多对多关系?
】如何在PostgreSQL中实现多对多关系?【英文标题】:Howtoimplementamany-to-manyrelationshipinPostgreSQL?【发布时间】:2012-04-0502:14:21【问题描述】:我相信标题是不言自明的。如何在PostgreSQL中创建表结构以建立多对多关系。我的例子:Pro... 查看详情
如何在 webapp 中实现 firebase 云消息传递? [关闭]
】如何在webapp中实现firebase云消息传递?[关闭]【英文标题】:Howtoimplementfirebasecloudmessaginginwebapp?[closed]【发布时间】:2016-06-3018:13:20【问题描述】:如果有人有任何示例应用程序可以在web应用程序中实现firebase云消息传递,这将... 查看详情