XMLRPC 中表示退出状态和实际结果的约定

发布于 2024-08-20 12:45:11 字数 390 浏览 10 评论 0原文

在C世界中,函数可以返回错误代码来表示退出状态,并使用INOUT/OUT参数来携带进程的实际结果。当涉及到xmlrpc时,没有INOUT/OUT参数,是否有任何最佳实践/约定来表示退出状态和实际结果?

上下文是我正在尝试编写一个在服务器上运行的代理/守护程序(python SimpleXMLRPCServer),并希望设计“协议”与其交互。

任何建议表示赞赏。

编辑: 根据 S.Lott 的评论,使问题更加清晰。

  • 这更多的是关于操作系统约定而不是 比 C 约定。我同意这一点。

  • 代理的工作或多或少是在服务器上运行一些cmd,本质上带有退出代码/结果习惯用法

    代理的工作

in the C world, a function can return error code to represent the exit status, and use INOUT/OUT parameter to carry the actual fruit of the process. when it comes to xmlrpc, no INOUT/OUT parameter, is there any best practice/conventions to represent the exit status and actual result?

the context is i am trying to write an agent/daemon (python SimpleXMLRPCServer) running on the Server, and want to design the "protocol" to interact with it.

any advice is appreciated.

EDIT:
per S.Lott's comment, make the problem more clear.

  • it is more about os convention rather
    than C convention. I agree with that.

  • the job of the agent is more or less run some cmd on the server, inherently with an exit code/result idiom

.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

A君 2024-08-27 12:45:11

在 Python 中实现这一点的一种简单方法是使用元组。让您的函数返回一个元组:(status, result),其中状态可以是数字或字符串,结果可以是您喜欢的任何 Python 数据结构。

这是一个改编自模块文档的示例。服务器代码:

from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

# Create server
server = SimpleXMLRPCServer(("localhost", 8000),
                            requestHandler=RequestHandler)

def myfunction(x, y):
    status = 1
    result = [5, 6, [4, 5]]
    return (status, result)
server.register_function(myfunction)

# Run the server's main loop
server.serve_forever()

客户端代码:

import xmlrpclib

s = xmlrpclib.ServerProxy('http://localhost:8000')
print s.myfunction(2, 4)

服务器函数返回一个元组

One simple way to implement this in Python is with a tuple. Have your function return a tuple of: (status, result) where the status can be numeric or a string, and the result can be any Python data structure you fancy.

Here's an example, adapted from the module documentation. Server code:

from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

# Create server
server = SimpleXMLRPCServer(("localhost", 8000),
                            requestHandler=RequestHandler)

def myfunction(x, y):
    status = 1
    result = [5, 6, [4, 5]]
    return (status, result)
server.register_function(myfunction)

# Run the server's main loop
server.serve_forever()

Client code:

import xmlrpclib

s = xmlrpclib.ServerProxy('http://localhost:8000')
print s.myfunction(2, 4)

The server function returns a tuple

野味少女 2024-08-27 12:45:11

“在 C 世界中,函数可以返回错误代码来表示退出状态,并使用 INOUT/OUT 参数来携带进程的实际结果”

  1. 将退出状态视为黑客。这不是 C 主义,而是 Linux 主义。 C 函数仅返回一个值。 C 没有例外,因此有多种方法来指示失败,但都非常糟糕。

    需要异常处理。 Python和Java有这个,而且它们不需要退出状态。

    然而,操作系统仍然依赖于退出状态,因为 shell 脚本仍然非常原始,并且某些语言(如 C)无法产生异常。

  2. 将输入/输出变量视为一种黑客行为。这是一个可怕的黑客行为,因为该函数除了返回值之外还有多个副作用。

这两个“功能”并不是真正值得遵循的最佳设计模式。

理想情况下,函数是“幂等的”——无论调用它多少次,都会得到相同的结果。输入/输出变量以模糊且难以调试的方式破坏幂等性。

您实际上并不需要这些功能中的任何一个,这就是为什么您看不到许多实现它们的最佳实践。

最佳实践是返回值或引发异常。如果您需要返回多个值,则返回一个元组。如果事情不起作用,您不会返回退出状态,而是会引发异常。


更新。由于远程进程基本上是运行远程命令的 RSH,因此您应该执行 remctl 的操作。

你需要模仿: http://linux.die.net/man/1/remctl 准确地说。你必须编写一个 Python 客户端和服务器。服务器返回一条带有状态代码的消息(以及任何其他摘要,例如运行时)。客户端以相同的状态代码退出。

"in the C world, a function can return error code to represent the exit status, and use INOUT/OUT parameter to carry the actual fruit of the process"

  1. Consider an exit status to be a hack. It's not a C-ism, it's a Linux-ism. C functions return exactly one value. C doesn't have exceptions, so there are several ways to indicate failure, all pretty bad.

    Exception handling is what's needed. Python and Java have this, and they don't need exit status.

    OS's however, still depend on exit status because shell scripting is still very primitive and some languages (like C) can't produce exceptions.

  2. Consider in/out variables also to be a hack. This is a terrible hack because the function has multiple side-effects in addition to returning a value.

Both of these "features" aren't really the best design patterns to follow.

Ideally, a function is "idempotent" -- no matter how many times you call it, you get the same results. In/Out variables break idempotency in obscure, hard-to-debug ways.

You don't really need either of these features, that's why you don't see many best practices for implementing them.

The best practice is to return a value or raise an exception. If you need to return multiple values you return a tuple. If things didn't work, you don't return an exit status, you raise an exception.


Update. Since the remote process is basically RSH to run a remote command, you should do what remctl does.

You need to mimic: http://linux.die.net/man/1/remctl precisely. You have to write a Python client and server. The server returns a message with a status code (and any other summary, like run-time). The client exits with that same status code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文