# How to run a function exactly once on each node?

**URL:** <https://discuss.ray.io/t/how-to-run-a-function-exactly-once-on-each-node/2178>\
**Category:** Ray Core\
**Created:** [May 17, 2021, 5:09pm UTC](https://discuss.ray.io/t/how-to-run-a-function-exactly-once-on-each-node/2178 "2021-05-17T17:09:01Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![sangcho](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sangcho/32/425_2.png) [@sangcho](https://discuss.ray.io/u/sangcho)\
**Post date:** [May 18, 2021, 4:08am UTC](https://discuss.ray.io/t/how-to-run-a-function-exactly-once-on-each-node/2178/5 "2021-05-18T04:08:37Z")

</div>

My recommendation is

```auto
@ray.remote(num_cpus=1)
class FunctionExecutor
def get_ip_addr(self):
    return "haha" # write your code

num_nodes = len(ray.nodes())
bundles = [{"CPU": 1} for _ in num_nodes]
pg = placement_group(bundles=bundles, strategy="STRICT_SPREAD")
ray.get(pg.ready())
executors = [FunctionExecutor.options(placement_group=pg).remote() for num_nodes]
ips = ray.get([executor.get_ip_addr.remote() for executor in executors])

```

Note that the placement group pre-reserves resources, so if you allocate 16 cpus for each bundle, it will probably take up the whole resources in every machine in your cluster (so you cannot execute other functions). If you are only looking for executing one functions with the placement group, you can use your approach

---

_[View the full topic](https://discuss.ray.io/t/how-to-run-a-function-exactly-once-on-each-node/2178)._
