.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples\gallery_examples\001_initialize_server_and_deal_with_license.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_gallery_examples_001_initialize_server_and_deal_with_license.py: .. _initialize_server_and_deal_with_license: Initialize PyAnsys Sound and check out the license -------------------------------------------------- This example shows how to initialize PyDPF-Core, load DPF Sound, and check out the required Ansys license (``increment avrxp_snd_level1``) only once. It also shows how to connect to the DPF server, verify where it is located, and get other useful information. This example also demonstrates the use of the LicenseContextManager, a mechanism that lets you check out the license only once for the duration of the session, which greatly improves performance. It shows the execution time of a simple DPF Sound operator when you do not use the LicenseContextManager versus when you do use it. **Prerequisites** Ensure that you have installed PyDPF-Core and DPF Sound according to procedures in the PyDPF-Core documentation: - If you have installed the latest Ansys release, see `Install using pip `_. - If you want to use the DPF standalone version, see `Install DPF Server `_. .. GENERATED FROM PYTHON SOURCE LINES 53-56 Perform required imports ~~~~~~~~~~~~~~~~~~~~~~~~ Perform the required imports: .. GENERATED FROM PYTHON SOURCE LINES 56-65 .. code-block:: Python # Load Ansys and other libraries. import datetime from ansys.sound.core.examples_helpers import download_flute_wav from ansys.sound.core.server_helpers import connect_to_or_start_server from ansys.sound.core.signal_utilities import LoadWav .. GENERATED FROM PYTHON SOURCE LINES 69-76 Use a DPF server without a LicenseContextManager ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Initialize a DPF server without using a LicenseContextManager. **Note**: When ``use_license_context=False``, the license is checked out each time you use a DPF Sound operator. .. GENERATED FROM PYTHON SOURCE LINES 76-90 .. code-block:: Python # Connect to a remote server or start a local server without using a LicenseContextManager print("Connecting to the server without using a LicenseContextManager") my_server, my_license_context = connect_to_or_start_server(use_license_context=False) # Check if you are using a local or remote server is_server_local = not my_server.has_client() print(f"Local server: {is_server_local}") # If using a local server, display the path to the server if is_server_local == True: print(f"Local server path (server variable): {my_server.ansys_path}") .. rst-class:: sphx-glr-script-out .. code-block:: none Connecting to the server without using a LicenseContextManager Local server: False .. GENERATED FROM PYTHON SOURCE LINES 91-92 Display information about the server that you are using. .. GENERATED FROM PYTHON SOURCE LINES 92-94 .. code-block:: Python print(f"Server information: {my_server.info}") .. rst-class:: sphx-glr-script-out .. code-block:: none Server information: {'server_ip': '172.27.161.234', 'server_port': 50052, 'server_process_id': 1788, 'server_version': '11.0', 'os': 'nt', 'path': None} .. GENERATED FROM PYTHON SOURCE LINES 95-97 Execute the PyAnsys Sound ``LoadWav`` operator several times in a row and measure the execution time. .. GENERATED FROM PYTHON SOURCE LINES 97-113 .. code-block:: Python path_flute_wav = download_flute_wav(server=my_server) for i in range(5): now = datetime.datetime.now() wav_loader = LoadWav(path_flute_wav) wav_loader.process() fc_signal = wav_loader.get_output() later = datetime.datetime.now() execution_time = later - now print( f"Elapsed time (loop {i+1}): " f"{execution_time.seconds + execution_time.microseconds/1e6}" f" seconds" ) .. rst-class:: sphx-glr-script-out .. code-block:: none Elapsed time (loop 1): 10.580335999999999 seconds Elapsed time (loop 2): 3.9371650000000002 seconds Elapsed time (loop 3): 3.887328 seconds Elapsed time (loop 4): 3.8792619999999998 seconds Elapsed time (loop 5): 3.8902609999999997 seconds .. GENERATED FROM PYTHON SOURCE LINES 114-115 Disconnect (shut down) the server. .. GENERATED FROM PYTHON SOURCE LINES 115-118 .. code-block:: Python print("Disconnecting from the server.") my_server = None .. rst-class:: sphx-glr-script-out .. code-block:: none Disconnecting from the server. .. GENERATED FROM PYTHON SOURCE LINES 119-126 Use a DPF server with a LicenseContextManager ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Initialize a DPF server using a LicenseContextManager and execute the same code as run previously. **Note**: The LicenseContextManager is a mechanism that checks out a license increment when entering the context and releases it when exiting the context. .. GENERATED FROM PYTHON SOURCE LINES 126-147 .. code-block:: Python # Connect to a remote server or start a local server using a LicenseContextManager print("Connecting to the server using a LicenseContextManager") my_server, my_license_context = connect_to_or_start_server(use_license_context=True) # Execute the same and measure the execution time path_flute_wav = download_flute_wav(server=my_server) for i in range(5): now = datetime.datetime.now() wav_loader = LoadWav(path_flute_wav) wav_loader.process() fc_signal = wav_loader.get_output() later = datetime.datetime.now() execution_time = later - now print( f"Elapsed time (loop {i+1}): " f"{execution_time.seconds + execution_time.microseconds / 1e6}" f" seconds" ) .. rst-class:: sphx-glr-script-out .. code-block:: none Connecting to the server using a LicenseContextManager Elapsed time (loop 1): 0.149384 seconds Elapsed time (loop 2): 0.145274 seconds Elapsed time (loop 3): 0.149516 seconds Elapsed time (loop 4): 0.13869 seconds Elapsed time (loop 5): 0.147305 seconds .. GENERATED FROM PYTHON SOURCE LINES 148-151 You can see that the execution time is much faster when you use a LicenseContextManager. This is because when a LicenseContactManager is not used, the license is checked out each time you use a DPF Sound operator. .. GENERATED FROM PYTHON SOURCE LINES 153-154 You can release the license increment by deleting the LicenseContextManager object. .. GENERATED FROM PYTHON SOURCE LINES 154-157 .. code-block:: Python print("Releasing the license increment by deleting the LicenseContextManager object.") my_license_context = None .. rst-class:: sphx-glr-script-out .. code-block:: none Releasing the license increment by deleting the LicenseContextManager object. .. GENERATED FROM PYTHON SOURCE LINES 158-161 Now that the LicenseContextManager has been deleted, any new call to a PyAnsys Sound function will spend time checking out the license increment again. Let us call the same function as before and measure the execution time again: .. GENERATED FROM PYTHON SOURCE LINES 161-171 .. code-block:: Python now = datetime.datetime.now() wav_loader = LoadWav(path_flute_wav) wav_loader.process() fc_signal = wav_loader.get_output() later = datetime.datetime.now() execution_time = later - now print( f"Elapsed time: " f"{execution_time.seconds + execution_time.microseconds / 1e6}" f" seconds", ) .. rst-class:: sphx-glr-script-out .. code-block:: none Elapsed time: 3.867432 seconds .. GENERATED FROM PYTHON SOURCE LINES 172-173 Disconnect (shut down) the server. .. GENERATED FROM PYTHON SOURCE LINES 173-175 .. code-block:: Python print("Disconnecting from the server and releasing the license increment.") my_server = None .. rst-class:: sphx-glr-script-out .. code-block:: none Disconnecting from the server and releasing the license increment. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 34.887 seconds) .. _sphx_glr_download_examples_gallery_examples_001_initialize_server_and_deal_with_license.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 001_initialize_server_and_deal_with_license.ipynb <001_initialize_server_and_deal_with_license.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 001_initialize_server_and_deal_with_license.py <001_initialize_server_and_deal_with_license.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 001_initialize_server_and_deal_with_license.zip <001_initialize_server_and_deal_with_license.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_