- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 Electron 应用程序前端 (zerorpc-node) 与 python 后端 (zerorpc-python) 通信,需要:
a) 能够向后端发送请求[标准zerorpc调用]
b) 能够同时运行多个后端进程[遵循 https://github.com/0rpc/zerorpc-node/issues/96 中的架构] )
c) 能够随意取消后端进程[不确定如何使用当前架构执行此操作]
任何关于如何为 (c) 构建解决方案的指导都会很棒。如有必要,如果 zerorpc 有限制,我愿意放弃它,但如果解决方案涉及使用 zerorpc,那就太棒了。
最佳答案
我最终使用 gipc
来启动进程。取消机制依赖于这样一个事实,即当 gipc
进程终止时,管道将关闭。整个 API 很复杂,这就是我最终得到的:
class ZerorpcService():
def __init__(self):
self.participant_id = None
self.extraction_methods = []
# maps pid to (process, pipe writer)
self.processes = {}
self. = lock.Semaphore()
def _launch_process(self, function, kwargs):
"""
Launches a new process
"""
try:
# add required arguments
pid = kwargs["pid"]
# start independent gipc process, communicated via pipe
started = False
with gipc.pipe() as (r, w):
with self.mutex:
if pid in self.processes:
return_value = {'status': 1, 'error': 'pid already exists', "report": True}
return
proc = gipc.start_process(self._process_wrapper, args=(function, kwargs, w))
self.processes[pid] = proc
started = True
# wait for process to send something over pipe
return_value = r.get()
except EOFError as eof:
# happens when we terminate a process because the pipe closes
return_value = {'status': 1, 'error': "pid {} terminated".format(pid), "report": False}
except Exception as error:
logging.exception(error)
return_value = {'status': 1, 'error': str(error), 'traceback': traceback.format_exc(), "report": True}
finally:
# deletes the pid from the map
with self.mutex:
if started:
del self.processes[pid]
return return_value
@staticmethod
def _process_wrapper(function, kwargs, pipe):
"""
Executes f with kwargs and formats the result into a dict.
Wraps it in error handling.
Routes the return value through the pipe provided.
"""
return_val = {'status': 0}
try:
raw_val = function(**kwargs)
if raw_val is not None:
return_val = raw_val
except Exception as error:
logging.exception(error)
return_val = {'status': 1, 'error': str(error), 'traceback': traceback.format_exc(), "report": True}
finally:
pipe.put(return_val)
def cancel_process(self, pid):
if pid in self.processes:
with self.mutex:
process = self.processes[pid]
if process.is_alive():
process.terminate()
return {'status': 0}
else:
return {'status': 1, 'error': 'pid {} not found'.format(pid), "traceback": traceback.format_exc(),
"report": True}
关于python - 可取消后端调用的架构推荐 [zerorpc if possible?],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56958867/
我想在我的服务器之间建立一个基于事件的系统。例如,当包装我的数据库逻辑的服务器更改状态时,我希望它通知我的其他服务器。发布/订阅设计似乎很适合这个,我听说过有关 ZeroRPC 的好消息。 有些人提到
我正在为一个项目使用 ZeroRPC,其中可能有多个实例在同一台机器上运行。出于这个原因,我需要能够自动分配未使用的端口号。我知道如何使用 socket.bind(('', 0)) 使用常规套接字或使
因此,我正在为一个宠物项目使用 ZeroRPC 和 Tornado,在将 ZeroRPC 与 python 的多处理库结合使用时遇到了一些问题。具体来说,我正在以编程方式创建和运行新的 ZeroRPC
我正在使用 Python 2.7 和 zeroRPC使客户端和服务器进行通信。我希望客户端向服务器发送请求,我希望服务器发送响应以确认它已收到请求。但后来我希望服务器对该请求执行一些繁重的计算。这些计
我正在尝试从 ZeroRPC website 运行 Python 服务器/node.js 客户端 HelloWorld 示例。所有相关的库似乎都已正确安装,但是在运行示例时出现错误: { name:
您可能知道,ZeroRPC 文档很少。我无法在 Python 服务器和 Node 客户端之间进行 Streaming 工作。 这是Python方法: @zerorpc.stream def Publi
目前,我正在使用 ZeroRPC,我让“工作人员”连接到“服务器”并执行服务器发送给他们的工作。 目前,只要有调用,就会通过 ZeroRPC 进行调用,据我所知,它使用的是 FIFO 队列。 我想使用
我正在用 python 设计一个简单的分布式数据库。我考虑用 ZeroRPC 实现通信层。 key 查找是通过 DHT 协议(protocol)以 req/rep 模式实现的。但是,我还希望能够通过键
我真的很想使用优秀的 zerorpc对于我使用 IronPython 的项目,但它似乎不受支持。 我已经尝试下载 zerorpc 的源代码 zip 并运行 "C:\Program Files (x86
我有一个 Electron 应用程序前端 (zerorpc-node) 与 python 后端 (zerorpc-python) 通信,需要: a) 能够向后端发送请求[标准zerorpc调用] b)
我正在尝试使用 ZeroRPC python 服务器和 node.js 客户端进行一些简单的负载测试。我注意到,如果请求花费的时间超过 10 秒,我将得不到任何数据。我尝试在python代码中配置无心
如何使用我的 zerorpc 维护不同的 session 或本地状态服务器? 例如(下图),如果我有多个客户端,后续客户端将覆盖模型状态。我考虑过每个客户端都有一个 ID,RPC 逻辑将尝试以这种方式
我一直在尝试在 Ubuntu 上安装 ZeroRPC。我已经安装了 ZeroMQ,但在安装 ZeroRPC 时出现了这些错误。我基本上是在后台从 node.js 运行一个 python 脚本,它发送
我的代码比这多得多,所以我将其精简到看起来相关的部分。根据记录的示例,我有一个供 ZeroRPC 使用的 python 类: import zerorpc, sys, signal class MyC
我需要安装 zerorpc .正如文档中提到的,我首先安装了 zeromq然后尝试了这个命令:npm install -g zerorpc但我收到此错误:- C:\WINDOWS\system32>n
我的设置有一个 Node.js 子 Node ,它创建了一个带有 Python 对象的 2-way ZeroRPC session 对。 python端类似这样: class MyClass:
我需要从 nodejs 调用一个 python 脚本并取回结果。我找到了 zerorpc看起来很合适的库。 python 脚本返回一个字符串数组,但在 Node 中我得到了二进制数据的对象。 这是 p
我正在尝试使用 Node.js 设置 zerorpc。但是,即使安装了 zerorpc 后,我仍然收到错误: Exception: ReferenceError: Can't find variabl
我想尝试 zeroRPC 但无法正确安装包。我在 Windows 7 下使用最新的 python_xy 发行版(python 2.7.3),我必须说我没有太多安装新模块的经验,因为发行版已经非常完整。
由于历史原因,我们正在使用 Heroku,我有一个非常棒的基于 ZeroRPC 的服务器,我很乐意将它放在 Heroku 服务上。对于这些基于“云”的平台所施加的限制,我有点天真,但大多数不允许打开任
我是一名优秀的程序员,十分优秀!