# How to create a global variable / lock when using the Ray trainer?

**URL:** <https://discuss.ray.io/t/how-to-create-a-global-variable-lock-when-using-the-ray-trainer/8079>\
**Category:** Uncategorized\
**Created:** [October 31, 2022, 12:11am UTC](https://discuss.ray.io/t/how-to-create-a-global-variable-lock-when-using-the-ray-trainer/8079 "2022-10-31T00:11:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vedant\_Roy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/vedant_roy/32/3234_2.png) [@Vedant\_Roy](https://discuss.ray.io/u/Vedant_Roy)\
**Post date:** [October 31, 2022, 12:11am UTC](https://discuss.ray.io/t/how-to-create-a-global-variable-lock-when-using-the-ray-trainer/8079/1 "2022-10-31T00:11:28Z")

</div>

I’m trying to use Ray with torchsnapshot: [GitHub - pytorch/torchsnapshot: A performant, memory-efficient checkpointing library for PyTorch applications, designed with large, complex distributed workloads in mind.](https://github.com/pytorch/torchsnapshot).  
For this to work, all processes must call `Snapshot.take` at the same time:

> TorchSnapshot supports distributed applications as first class citizens. To take a snapshot of a distributed application, simply invoke [`Snapshot.take()`](https://pytorch.org/torchsnapshot/stable/api_reference.html#torchsnapshot.Snapshot.take) on all ranks simultaneously (similar to calling a torch.distributed API). The persisted application state will be organized as a single snapshot.

I’m trying to setup my code such that–when an EC2 instance receives an instance termination warning–the code will take a snapshot.

For this to work, I need some sort of global variable that can be turned on/off to indicate that a snapshot should be taken.

Would the correct thing to use here be a Ray named actor?

---

<div class="post-metadata">

**Author:** ![Yard1](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/yard1/32/508_2.png) [@Yard1](https://discuss.ray.io/u/Yard1)\
**Post date:** [October 31, 2022, 11:22pm UTC](https://discuss.ray.io/t/how-to-create-a-global-variable-lock-when-using-the-ray-trainer/8079/2 "2022-10-31T23:22:34Z")

</div>

A named actor would work here. You can also create a normal actor, and pass a reference to it in the train loop config.
