tf.raw_ops.MutexLock
Stay organized with collections
Save and categorize content based on your preferences.
Locks a mutex resource.
tf.raw_ops.MutexLock(
mutex, name=None
)
The output is the lock. So long as the lock tensor
is alive, any other request to use MutexLock
with this mutex will wait.
This is particularly useful for creating a critical section when used in
conjunction with MutexLockIdentity
:
mutex = mutex_v2(
shared_name=handle_name, container=container, name=name)
def execute_in_critical_section(fn, *args, **kwargs):
lock = gen_resource_variable_ops.mutex_lock(mutex)
with ops.control_dependencies([lock]):
r = fn(*args, **kwargs)
with ops.control_dependencies(nest.flatten(r)):
with ops.colocate_with(mutex):
ensure_lock_exists = mutex_lock_identity(lock)
# Make sure that if any element of r is accessed, all of
# them are executed together.
r = nest.map_structure(tf.identity, r)
with ops.control_dependencies([ensure_lock_exists]):
return nest.map_structure(tf.identity, r)
While fn
is running in the critical section, no other functions which wish to
use this critical section may run.
Often the use case is that two executions of the same graph, in parallel,
wish to run fn
; and we wish to ensure that only one of them executes
at a time. This is especially important if fn
modifies one or more
variables at a time.
It is also useful if two separate functions must share a resource, but we
wish to ensure the usage is exclusive.
Args |
mutex
|
A Tensor of type resource . The mutex resource to lock.
|
name
|
A name for the operation (optional).
|
Returns |
A Tensor of type variant .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.raw_ops.MutexLock\n\n\u003cbr /\u003e\n\nLocks a mutex resource.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.raw_ops.MutexLock`](https://www.tensorflow.org/api_docs/python/tf/raw_ops/MutexLock)\n\n\u003cbr /\u003e\n\n tf.raw_ops.MutexLock(\n mutex, name=None\n )\n\nThe output is the lock. So long as the lock tensor\n\nis alive, any other request to use `MutexLock` with this mutex will wait.\n\nThis is particularly useful for creating a critical section when used in\nconjunction with `MutexLockIdentity`: \n\n\n mutex = mutex_v2(\n shared_name=handle_name, container=container, name=name)\n\n def execute_in_critical_section(fn, *args, **kwargs):\n lock = gen_resource_variable_ops.mutex_lock(mutex)\n\n with ops.control_dependencies([lock]):\n r = fn(*args, **kwargs)\n\n with ops.control_dependencies(nest.flatten(r)):\n with ops.colocate_with(mutex):\n ensure_lock_exists = mutex_lock_identity(lock)\n\n # Make sure that if any element of r is accessed, all of\n # them are executed together.\n r = nest.map_structure(tf.identity, r)\n\n with ops.control_dependencies([ensure_lock_exists]):\n return nest.map_structure(tf.identity, r)\n\nWhile `fn` is running in the critical section, no other functions which wish to\nuse this critical section may run.\n\nOften the use case is that two executions of the same graph, in parallel,\nwish to run `fn`; and we wish to ensure that only one of them executes\nat a time. This is especially important if `fn` modifies one or more\nvariables at a time.\n\nIt is also useful if two separate functions must share a resource, but we\nwish to ensure the usage is exclusive.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------|------------------------------------------------------------|\n| `mutex` | A `Tensor` of type `resource`. The mutex resource to lock. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor` of type `variant`. ||\n\n\u003cbr /\u003e"]]