Note
Go to the end to download the full example code.
Setup#
This prepares the environment for running the examples.
Description#
Perform the following steps to set up the environment.
import os
import sys
from time import monotonic, sleep
# Add the 'examples' directory to the Python path to allow importing files in that directory.
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(os.getcwd()))))
from examples.examples_globals import (
SHERLOCK_COMMAND_ARGS,
SHERLOCK_CONNECT_TIMEOUT,
SHERLOCK_LAUNCH_AND_CONNECT_TIMEOUT,
SHERLOCK_PORT,
SHERLOCK_TRANSPORT_MODE,
store_sherlock_tutorial_path,
)
from ansys.sherlock.core import launcher
from ansys.sherlock.core.errors import SherlockCannotUsePortError, SherlockConnectionError
# attempt to exit Sherlock in case a previous run was not properly terminated
try:
sherlock = launcher.connect(
port=SHERLOCK_PORT, timeout=SHERLOCK_CONNECT_TIMEOUT, transport_mode=SHERLOCK_TRANSPORT_MODE
)
print(f"Exiting Sherlock that is running on port {SHERLOCK_PORT}.")
sherlock.common.exit(True)
EXIT_TIMEOUT = 30
start_time = monotonic()
while True:
try:
launcher._is_port_available(port=SHERLOCK_PORT)
break
except SherlockCannotUsePortError as error:
if monotonic() - start_time >= EXIT_TIMEOUT:
raise TimeoutError(
f"Sherlock did not release port {SHERLOCK_PORT} "
f"within {EXIT_TIMEOUT} seconds."
) from error
sleep(0.5)
print("Sherlock exited successfully.")
except SherlockConnectionError:
# Sherlock is not running
pass
Launch Sherlock#
Launch Sherlock on Windows in headless mode using Windows Named User Authentication for the gRPC transport mode. See the API reference for how to launch Sherlock on Linux or Windows using other options.
sherlock, ansys_install_path = launcher.launch_and_connect(
port=SHERLOCK_PORT,
timeout=SHERLOCK_LAUNCH_AND_CONNECT_TIMEOUT,
transport_mode=SHERLOCK_TRANSPORT_MODE,
sherlock_command_args=SHERLOCK_COMMAND_ARGS,
)
# store the path to the Sherlock tutorial directory in a shared data file for use by other examples
store_sherlock_tutorial_path(ansys_install_path)
Traceback (most recent call last):
File "examples\00-setup\setup.py", line 92, in <module>
sherlock, ansys_install_path = launcher.launch_and_connect(
~~~~~~~~~~~~~~~~~~~~~~~~~~~^
port=SHERLOCK_PORT,
^^^^^^^^^^^^^^^^^^^
...<2 lines>...
sherlock_command_args=SHERLOCK_COMMAND_ARGS,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File ".venv\Lib\site-packages\ansys\sherlock\core\launcher.py", line 247, in launch_and_connect
sherlock = connect(
port=port,
...<3 lines>...
uds_id=uds_id,
)
File ".venv\Lib\site-packages\ansys\sherlock\core\launcher.py", line 326, in connect
raise e
File ".venv\Lib\site-packages\ansys\sherlock\core\launcher.py", line 308, in connect
_wait_for_sherlock_grpc_ready(channel, timeout)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
File ".venv\Lib\site-packages\ansys\sherlock\core\launcher.py", line 397, in _wait_for_sherlock_grpc_ready
raise SherlockConnectionError(message="Error starting gRPC service")
ansys.sherlock.core.errors.SherlockConnectionError: gRPC connection error: Error starting gRPC service
Total running time of the script: (0 minutes 22.289 seconds)