# Understanding @serve.deployment

**URL:** <https://discuss.ray.io/t/understanding-serve-deployment/15692>\
**Category:** Ray Serve\
**Created:** [August 29, 2024, 1:02pm UTC](https://discuss.ray.io/t/understanding-serve-deployment/15692 "2024-08-29T13:02:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![VIKTOR\_ODRIOZOLA\_EGA](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/viktor_odriozola_ega/32/6491_2.png) [@VIKTOR\_ODRIOZOLA\_EGA](https://discuss.ray.io/u/VIKTOR_ODRIOZOLA_EGA)\
**Post date:** [August 29, 2024, 1:02pm UTC](https://discuss.ray.io/t/understanding-serve-deployment/15692/1 "2024-08-29T13:02:20Z")

</div>

Dear all,

I am trying to understand how Ray Serve works. I did a simple Python script and used the corresponding decorator (@serve.deployment) to apply Ray Serve philosophy. However, when executing the script, an “Attribute” error appears.

------------------------------------------------------------ SCRIPT ------------------------------------------------------------

# father\_son.py script

# Import libraries

import random  
import ray  
import time  
import asyncio

from ray import serve

# Initialize Ray

ray.init(ignore\_reinit\_error=True)  
serve.start(detached=True)

# Father class that generates a vector of two random numbers

@serve.deployment  
class Father:  
def generate\_numbers(self):  
“”“Generate two random numbers between 0 and 9.”“”  
num1 = random.randint(0, 9)  
num2 = random.randint(0, 9)  
return [num1, num2]

# Son class that performs the multiplication of two numbers

@serve.deployment  
class Son:  
def multiply\_numbers(self, numbers):  
“”“Multiply two numbers from a given vector of two elements.”“”  
if len(numbers) != 2:  
raise ValueError(“Input must be a vector of two elements.”)  
return numbers[0] \* numbers[1]

def main():  
# Deploy Father and Son using serve.run with deployment options  
Father.deploy()  
Son.deploy()

```
# Get handles to deployed services
father = Father.get_handle()
son = Son.get_handle()

# Function to perform workflow
async def do_workflow():
    numbers = await father.generate_numbers.remote()
    result = await son.multiply_numbers.remote(numbers)
    print(f"Numbers: {numbers}, Product: {result}")

# Run the workflows in parallel
(do_workflow() for _ in range(10))

```

if **name** == “ **main** ”:  
start\_time = time.time()

```
# Run the main function
main()

end_time = time.time()
time_taken = end_time - start_time
print(f"Time taken to execute the code in parallel: {time_taken}")

ray.shutdown()

```

------------------------------------------------------------ /SCRIPT ------------------------------------------------------------

---------------------------------------- CMD OUTPUT ----------------------------------------  
odriovik@odriovik:~/Escritorio/Python$ python3 father\_son\_ray\_serve.py  
2024-08-29 14:47:15,423 INFO worker.py:1772 – Started a local Ray instance. View the dashboard at [http://127.0.0.1:8265](http://127.0.0.1:8265)  
(ProxyActor pid=11615) INFO 2024-08-29 14:47:18,240 proxy 10.0.2.15 proxy.py:1179 - Proxy starting on node d4dbd04f883aae16a82d7a8aa2639eee76da576121ad952c426a3d16 (HTTP port: 8000).  
2024-08-29 14:47:18,356 WARNING api.py:346 – The default value for `max_ongoing_requests` has changed from 100 to 5 in Ray 2.32.0.  
2024-08-29 14:47:18,356 WARNING api.py:397 – The default value for `max_ongoing_requests` has changed from 100 to 5 in Ray 2.32.0.  
2024-08-29 14:47:18,366 WARNING api.py:346 – The default value for `max_ongoing_requests` has changed from 100 to 5 in Ray 2.32.0.  
2024-08-29 14:47:18,367 WARNING api.py:397 – The default value for `max_ongoing_requests` has changed from 100 to 5 in Ray 2.32.0.  
Traceback (most recent call last):  
File “/home/odriovik/Escritorio/Python/father\_son\_ray\_serve.py”, line 55, in   
main()  
File “/home/odriovik/Escritorio/Python/father\_son\_ray\_serve.py”, line 35, in main  
Father.deploy()  
^^^^^^^^^^^^^  
AttributeError: ‘Deployment’ object has no attribute ‘deploy’. Did you mean: ‘\_deploy’?  
---------------------------------------- /CMD OUTPUT ----------------------------------------

I would appreciate it if someone helps me with this issue. Big thank you to Ray Community.

---

<div class="post-metadata">

**Author:** ![Gene](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/gene/32/4701_2.png) [@Gene](https://discuss.ray.io/u/Gene)\
**Post date:** [September 4, 2024, 9:03pm UTC](https://discuss.ray.io/t/understanding-serve-deployment/15692/2 "2024-09-04T21:03:04Z")

</div>

To deploy your Serve application, you would first need to call `bind()` to create the application object. Then you can call `serve.run` to deploy them. Basically instead of calling those `.deploy()` and `.get_handle()` you can do the following

```auto
# create Serve applications
father_app = Father.bind()
son_app = Son.bind()

# Deploy the application and get the handles
father_handle = serve.run(father_app)
son_handle = serve.run(son_app)

```

Then you can do your workflow and call those remote functions.

Also note you can pass the deployment handle in the constructors if you are trying to do model compositions. More details on this doc [Deploy Compositions of Models — Ray 2.35.0](https://docs.ray.io/en/latest/serve/model_composition.html#basic-deploymenthandle-example)
