Skip to content

Instantly share code, notes, and snippets.

@ayaka14732
Created September 19, 2022 06:59
Show Gist options
  • Save ayaka14732/85928ed4ff83318f3ab5ac1da263f0fa to your computer and use it in GitHub Desktop.
Save ayaka14732/85928ed4ff83318f3ab5ac1da263f0fa to your computer and use it in GitHub Desktop.
Check if Google Cloud TPU is in use
def is_tpu_in_use() -> bool:
import fcntl
from fcntl import LOCK_EX, LOCK_NB
import posix
from posix import O_CREAT, O_RDWR
import sys
# https://github.com/tensorflow/tensorflow/blob/1a05bad57a2eb870a561daba0255761e0a472d1d/tensorflow/core/tpu/tpu_initializer_helper.cc#L167-L171
fd = posix.open('/tmp/libtpu_lockfile', O_CREAT | O_RDWR, 0o644)
try:
fcntl.lockf(fd, LOCK_EX | LOCK_NB, 0)
except BlockingIOError:
return True
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment