Skip to content

PythonServices

Anson Strauch edited this page Jan 23, 2026 · 1 revision

Using Services in Python - Example 12

This example is a reimplementation of example 4 in Python. There's really not much to say here, as in the previous example the code is very similar to the code in the C++ version.

Similar to the previous example, it is all defined in one .py file.

The Service Provider and Requestor

One key difference in this example is that we create a class that is both the GravityServiceProvider and the GravityRequestor.

done = False
class MyRequestorProvider(GravityRequestor, GravityServiceProvider):
    # We need to explicitly initialize each parent here to allow swig to
    # configure the underlying proxy object correctly.
    def __init__(self):
        GravityRequestor.__init__(self)
        GravityServiceProvider.__init__(self)

    def requestFilled(self, serviceID, requestID, response):
        multResponse = MultiplicationResultPB()
        response.populateMessage(multResponse)
        SpdLog.warn("made it to request filled with request GDP ID = "+response.dataProductID +" and response = " + str(multResponse.result))
        global done
        done = True

    def request(self, serviceID, dataProduct):
        SpdLog.warn("made it to my request!")
        SpdLog.warn("for serviceID = "+serviceID)
        operands = MultiplicationOperandsPB()
        SpdLog.warn(str(type(operands)))
        dataProduct.populateMessage(operands)
        SpdLog.warn("have operands = "+str([operands.multiplicand_a, operands.multiplicand_b]))

        multResponse = MultiplicationResultPB()
        multResponse.result = operands.multiplicand_a * operands.multiplicand_b
        gdp = GravityDataProduct("MultResponse")
        gdp.data = multResponse
        SpdLog.warn("returning response with result = "+str(multResponse.result))
        return gdp

The requestFilled function is defined by the GravityRequestor class and is called when a request is filled by the GravityServiceProvider.

The request function defines what a request is for the GravityServiceProvider.

Now to use the ServiceProvider and Requestor. First you must register it as a service.

gn = GravityNode()
while gn.init("PyRequest") != gravity.SUCCESS:
    SpdLog.warn("failed to init, retrying...")
    time.sleep(1)

requestorProvider = MyRequestorProvider()
gn.registerService("Multiplication", gravity.TCP, requestorProvider)

Then you can make requests:

# Async request
operands = MultiplicationOperandsPB()
operands.multiplicand_a = 3
operands.multiplicand_b = 4
gdp = GravityDataProduct("MultRequest")
gdp.data = operands
gn.request("Multiplication", gdp, requestorProvider)

while not done:
    time.sleep(1)

The code above defines an asynchronous request. Once the gn.request is called, Gravity will asynchronously fill the request when it is ready. Gravity will call the requestFilled method.

Now a synchronous request:

# Sync request
operands.multiplicand_a = 5
operands.multiplicand_b = 6
gdp.data=operands
gdpResp = gn.request("Multiplication", gdp)
SpdLog.warn("received GDP response")
multResponse = MultiplicationResultPB()
gdpResp.populateMessage(multResponse)
SpdLog.warn("made it to request filled with request GDP ID = "+gdpResp.dataProductID +" and response = " + str(multResponse.result))

The code above defines a synchronous request. It will return the result of the request as a GravityDataProduct to be used by the user. The user must process the data.

Clone this wiki locally