English 中文(简体)
convention to represent the exit status and actual result in XMLRPC
原标题:

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

.

最佳回答

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

问题回答

"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.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签