# How to write file to worker's local storage in Kuberay

**URL:** <https://discuss.ray.io/t/how-to-write-file-to-workers-local-storage-in-kuberay/13075>\
**Category:** Ray Clusters\
**Created:** [December 7, 2023, 6:20am UTC](https://discuss.ray.io/t/how-to-write-file-to-workers-local-storage-in-kuberay/13075 "2023-12-07T06:20:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HaoCheng\_Xu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/haocheng_xu/32/4741_2.png) [@HaoCheng\_Xu](https://discuss.ray.io/u/HaoCheng_Xu)\
**Post date:** [December 7, 2023, 6:20am UTC](https://discuss.ray.io/t/how-to-write-file-to-workers-local-storage-in-kuberay/13075/1 "2023-12-07T06:20:26Z")

</div>

I’m using kuberay to do some large data processing job. I need to download some data and write some data as intermediate result. But when I use the following code. It seems all worker and head share same disk storage.

```auto
@ray.remote
def compute(num: int) -> int:
    path = f'/tmp/test.txt'
    if os.path.exists(path):
        raise FileExistsError('File already exists')
    with open(path, encoding='utf-8', mode='w') as f:
        f.write('#' * 1000)
        f.flush()
    return num**2

```

The ideal local storage for worker should be isolated and will be cleared up when the task completed

---

<div class="post-metadata">

**Author:** ![Jules\_Damji](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/jules_damji/32/4058_2.png) [@Jules\_Damji](https://discuss.ray.io/u/Jules_Damji)\
**Post date:** [December 7, 2023, 9:31pm UTC](https://discuss.ray.io/t/how-to-write-file-to-workers-local-storage-in-kuberay/13075/2 "2023-12-07T21:31:53Z")

</div>

@HaoCheng_Xu

If I understand correctly, each worker node will have its own `/tmp/path _file_name`, so if more than one `compute` function is scheduled distributed on worker process on a node will see the same file. If you want each function instance to have a unique file to avoid this collusion, then  
have the `compute` function create a tempfile, which will guranteed a unique file per instance of  
a `compute` function on the same node.

cc: @architkulkarni @Kai-Hsun_Chen

---

<div class="post-metadata">

**Author:** ![architkulkarni](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/architkulkarni/32/10_2.png) [@architkulkarni](https://discuss.ray.io/u/architkulkarni)\
**Post date:** [December 8, 2023, 12:01am UTC](https://discuss.ray.io/t/how-to-write-file-to-workers-local-storage-in-kuberay/13075/3 "2023-12-08T00:01:06Z")

</div>

I agree with Jules’s suggestion to use [tempfile — Generate temporary files and directories — Python 3.12.0 documentation](https://docs.python.org/3/library/tempfile.html). I think if each Ray task were to have its own isolated storage, the overhead might cause issues at scale. Plus, there are use cases where you want all tasks to share the same local filesystem.
