COMPAS云计算软件包
compas-cloud的Python项目详细描述
康柏云
compas云是compas.rpc
模块的进一步发展。它使用websockets而不是restfulapi来允许各种前端程序(如Rhino、GH、RhinoVault2、blender或基于web的查看器)之间的双向通信,这些程序在不同的环境中实现,包括CPython、IronPython和Javascript。它还允许在用户会话内部将某些变量保存到后端,以避免冗余数据传输造成的开销。在
安装
从源安装
git clone https://github.com/BlockResearchGroup/compas_cloud.git pip install -e .
为Rhino安装
^{pr2}$使用代理
运行服务器:
- 从命令行启动:
python -m compas_cloud.server
在 - 如果没有可连接的服务器,代理将自动在后台启动服务器。如果服务器以这种方式启动,它将继续在后台运行,如果以后创建新的代理,它将重新连接。在
基本用法
compas_cloud的主要目的之一是允许在更封闭的环境(如IronPython)中使用完整的compas功能。下面的示例演示如何通过代理使用基于numpy的COMPAS函数,该代理可以在Rhino等软件中运行:
basic.py
fromcompas_cloudimportProxyfromcompas.geometryimportTranslationproxy=Proxy()transform_points_numpy=proxy.function('compas.geometry.transform_points_numpy')# create a proxy funcitonpts=[[0,0,0],[1,0,0]]T=Translation([100,0,0]).matrixtransform_points_numpy(pts,T)# call the function through proxyprint(result)# will print: [[100.0, 0.0 ,0.0], [101.0, 0.0, 0.0]]
缓存
Compas_cloud允许在服务器端缓存数据或函数输出,而不是一直将它们发送到前端。这可以极大地提高涉及大量数据输入和输出的长迭代操作的性能。在
fromcompas_cloudimportProxyfromcompas.geometryimportTranslation# CACHING INPUT PARAMETERSproxy=Proxy()transform_points_numpy=proxy.function('compas.geometry.transform_points_numpy')# create a proxy funcitonpts=[[0,0,0],[1,0,0]]pts_cache=proxy.cache(pts)# cache the object to server side and return its referenceprint(pts_cache)# will print: {'cached': some_unique_id}T=Translation([100,0,0]).matrixresult=transform_points_numpy(pts_cache,T)# call the function through proxyprint(result)# will print: [[100.0, 0.0 ,0.0], [101.0, 0.0, 0.0]]# CACHING RETURNED DATAtransform_points_numpy=proxy.function('compas.geometry.transform_points_numpy',cache=True)# this function will now return a cache object instead of the actual datapts=[[0,0,0],[1,0,0]]pts_cache=proxy.cache(pts)print(pts_cache)# will print: {'cached': some_unique_id}T=Translation([100,0,0]).matrixresult_cache=transform_points_numpy(pts_cache,T)# call the function through proxyprint(result_cache)# will print: {'cached': some_unique_id}result=proxy.get(result_cache)# fetch the actual data of the cache objectprint(result)# will print: [[100.0, 0.0 ,0.0], [101.0, 0.0, 0.0]]
服务器控制
用户可以使用以下示例中的命令restart/check/shutdown
从代理服务器server_control.py
fromcompas_cloudimportProxyimporttimeprint("\n starting a new Proxy and by default starts a server in background")proxy=Proxy(background=True)time.sleep(3)print("\n restarting the background server and open a new one in a prompt console")proxy.background=Falseproxy.restart()time.sleep(3)print("\n check if the proxy is healthily connected to server")print(proxy.check())time.sleep(3)print("\n shut the the server and quite the program")proxy.shutdown()time.sleep(3)
其他示例
一个benchmark test比较纯python和numpy与缓存转换10k点100次:
python examples/benchmark.py
Iterative plotting带有回调的示例:
python examples/dr_numpy.py
Using non-compas packages like numpy with IronPython:
与Rhino一起运行examples/example_numpy.py
使用会话(当前仅适用于MacOS/Linux)
Compas_cloud.Sessions
是一个任务管理器类,它帮助执行一批持久的任务,如FEA和DEM模拟。它创建一个任务队列和一个工人集合来并行执行任务,并将程序日志保存到每个相应的位置。Sessions
可以在本地运行,也可以通过Proxy
在后台服务器中运行。在
示例
Running Sessions Locally:
python examples/sessions_local.py
fromcompas_cloudimportSessions# define a psuedo task that will take few seconds to finishdeffunc(a):importtimeforiinrange(a):time.sleep(1)print('sleeped ',i,'s')# initiate a session object, and specify where the logs will be stored and number of workers# if no log_path is given, all logs will be streamed to terminal and not saved# the default worker_num is equal to the number of cpus accessible on the computers=Sessions(log_path=None,worker_num=4)# add several tasks to the session using different parameterss.add_task(func,1)s.add_task(func,2)s.add_task(func,3)s.add_task(func,4)s.add_task(func,5)# kick of the taks and start to listen to the events when tasks start or finishs.start()s.listen()
您应该看到以下日志:
{'waiting': 5, 'running': 0, 'failed': 0, 'finished': 0, 'total': 5} ________ START
{'waiting': 5, 'running': 0, 'failed': 0, 'finished': 0, 'total': 5} ________ using 4 workers
{'waiting': 5, 'running': 0, 'failed': 0, 'finished': 0, 'total': 5} ________ worker 58884 started
{'waiting': 4, 'running': 1, 'failed': 0, 'finished': 0, 'total': 5} ________ task-0: started
{'waiting': 4, 'running': 1, 'failed': 0, 'finished': 0, 'total': 5} ________ worker 58885 started
{'waiting': 4, 'running': 1, 'failed': 0, 'finished': 0, 'total': 5} ________ task-0: streaming log to temp/task-0.log
{'waiting': 3, 'running': 2, 'failed': 0, 'finished': 0, 'total': 5} ________ task-1: started
...
{'waiting': 0, 'running': 0, 'failed': 0, 'finished': 5, 'total': 5} ________ task-4: finished
{'waiting': 0, 'running': 0, 'failed': 0, 'finished': 5, 'total': 5} ________ worker 58884 terminated
{'waiting': 0, 'running': 0, 'failed': 0, 'finished': 5, 'total': 5} ________ FINISHED
Running Sessions With Proxy:
python examples/sessions_remote.py
fromcompas_cloudimportProxy# define a psuedo task that will take few seconds to finishdeffunc(a):importtimeforiinrange(a):time.sleep(1)print('sleeped ',i,'s')# initiate a Sessions object through Proxy that connects to a background serverp=Proxy()s=p.Sessions()# add several tasks to the session using different parameterss.add_task(func,1)s.add_task(func,2)s.add_task(func,3)s.add_task(func,4)s.add_task(func,5)# kick of the taks and start to listen to the events when tasks start or finishs.start()s.listen()
您应该能够从上面的示例中看到相同的日志
- 项目
标签: