自从我上次更新 IoP 以来已经有一段时间了。 我们一起追吧!
IoP 命令行界面已添加重大增强功能:
-
名称更改:
grongier.pex
模块已重命名为iop
以与项目的新品牌保持一致。 - 异步支持: IoP 现在完全支持异步函数和协程。
项目重命名
grongier.pex
模块仍然可访问以实现向后兼容性,但将在未来版本中删除。 使用iop
模块进行新的开发。
异步功能
虽然 IoP 长期以来支持异步调用,但之前无法直接使用异步函数和协程。 在探索这个新功能之前,让我们先回顾一下 InterSystems IRIS 中的异步调用功能,并研究两个示例。
旧版异步调用
这说明了传统方法:
from iop import BusinessProcess from msg import MyMessage class MyBP(BusinessProcess): def on_message(self, request): msg_one = MyMessage(message="Message1") msg_two = MyMessage(message="Message2") self.send_request_async("Python.MyBO", msg_one, completion_key="1") self.send_request_async("Python.MyBO", msg_two, completion_key="2") def on_response(self, request, response, call_request, call_response, completion_key): if completion_key == "1": self.response_one = call_response elif completion_key == "2": self.response_two = call_response def on_complete(self, request, response): self.log_info(f"Received response one: {self.response_one.message}") self.log_info(f"Received response two: {self.response_two.message}")
这反映了 IRIS 中的异步调用行为。 send_request_async
向业务运营发送请求,on_response
处理收到的响应。 completion_key
区分响应。
同步多请求功能
虽然不是全新的,但同时发送多个同步请求的能力值得注意:
from iop import BusinessProcess from msg import MyMessage class MyMultiBP(BusinessProcess): def on_message(self, request): msg_one = MyMessage(message="Message1") msg_two = MyMessage(message="Message2") tuple_responses = self.send_multi_request_sync([("Python.MyMultiBO", msg_one), ("Python.MyMultiBO", msg_two)]) self.log_info("All requests have been processed") for target, request, response, status in tuple_responses: self.log_info(f"Received response: {response.message}")
此示例同时向同一个业务操作发送两个请求。响应是一个包含每个调用的目标、请求、响应和状态的元组。当请求顺序不重要时,这特别有用。
异步函数和协程
以下是如何在 IoP 中利用异步函数和协程:
import asyncio from iop import BusinessProcess from msg import MyMessage class MyAsyncNGBP(BusinessProcess): def on_message(self, request): results = asyncio.run(self.await_response(request)) for result in results: print(f"Received response: {result.message}") async def await_response(self, request): msg_one = MyMessage(message="Message1") msg_two = MyMessage(message="Message2") tasks = [self.send_request_async_ng("Python.MyAsyncNGBO", msg_one), self.send_request_async_ng("Python.MyAsyncNGBO", msg_two)] return await asyncio.gather(*tasks)
这会使用 send_request_async_ng
同时发送多个请求。 asyncio.gather
确保同时等待所有响应。
如果你已经跟进到这里了,请评论“Boomerang”! 这意义重大。谢谢!
await_response
是一个发送多个请求并等待所有响应的协程。
使用异步函数和协程的优点包括通过并行请求提高性能、增强可读性和可维护性、使用 asyncio
模块提高灵活性以及更好的异常和超时处理。
异步方法的比较
send_request_async
、send_multi_request_sync
和 send_request_async_ng
之间的主要区别是什么?
-
send_request_async
:仅当实现on_response
并使用completion_key
时才发送请求并等待响应。 简单,但并行请求的可扩展性较差。 -
send_multi_request_sync
:同时发送多个请求并等待所有响应。易于使用,但不保证响应顺序。 -
send_request_async_ng
:同时发送多个请求并等待所有响应,保持响应顺序。需要异步函数和协程。
多线程快乐!
以上是Python 更新异步支持的互操作性的详细内容。更多信息请关注PHP中文网其他相关文章!

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)