Use of launch class instance outside python

From Logic API documentation:

classmethod launch( application_path=None , connect_timeout_seconds=None , grpc_channel_arguments=None , port=None )

Launch the Logic2 application and shut it down when the returned Manager is closed.

when I try to use launch class method just to load the logic application, the logic application loads but it eventually dies as soon as the program comes out of the scope of the function.

To avoid this I need to keep using the instance of launch class to connect to Logic analyzer and start capturing data.

But my use case is different, I just need to launch the Logic application in one python function which is invoked by a perl script. Then my perl script performs some other steps and later tries to connect and capture data on digital channels.
Why am I doing this is because Logic application takes some time to launch whereas I cannot wait for it to launch. Therefore I need to keep it running so that I do not miss my event when the event arrives.
Could you please provide some improvement in the form of python code so that I can perform my desired task.

What do you do with the Manager returned from the launch() method? Does your custom function return this Manager back to the caller, or is it only local to that custom function only?

Any code snippets to better explain how you are doing things?

Regardless, I think the API will close the launched instance when the Manager is automatically destroyed after it goes out of scope, or if you explicitly close() it yourself. So, I think you either need to preserve it (e.g., by returning it or making it global), or use the connect() vs. launch() method and start the Logic 2 process another way.

Following is my launch function.
from saleae import automation
import os
import time
import json
import argparse

def launch(path_to_exe, timeout, la_port):
“”"
Launch the Logic2 application.

Parameters:
    path_to_exe (str): Path to the Logic2 executable.
    timeout (int): Timeout for the launch in seconds.
    la_port (int): Port to connect the Logic Analyzer.

Returns:
    app: Logic2 application instance or None if launch fails.
"""
try:
    app = automation.Manager.launch(application_path=path_to_exe, connect_timeout_seconds=timeout, grpc_channel_arguments=None, port=la_port)
    time.sleep(2)
    return app
except Exception as e:
    print(f"Error launching Logic2 application: {e}")
    return None

if name == “main”:
description_text = “A python script that contains various methods using Saleae’s Python API to perform Logic operations”
example_text = “Example usage: python script.py --start_capture --logic_ip 127.0.0.1 --logic_port 10430 …”

parser = argparse.ArgumentParser(description=description_text, epilog=example_text, formatter_class=argparse.RawDescriptionHelpFormatter)

# Apllication launch group
launch_group = parser.add_argument_group("Logic2 application launch", "Launch Logic2 application before data acquisition")
launch_group.add_argument('--launch', action='store_true', help='Launch Logic2 application')
launch_group.add_argument('--exe_path', default=None, type=str, help='Path where Logic2 exe is placed')
launch_group.add_argument('--timeout', default=10.0, type=float, help='Number of seconds to attempt to connect to gRPC server')
launch_group.add_argument('--la_port', default=10430, type=int, help='Port of the Logic Analyzer')

args = parser.parse_args()

if args.launch:
    if not launch(args.exe_path, args.timeout, args.port):
        exit()

Following is my perl code:
sub StartLogic_exe {
my ($path, $timeout, $port) = @_;

$path        //= undef;
$timeout     //= 10.0;
$port        //= 10430;

my $pycli = "Path/to/python.exe";
my $py_script = "../tools/logic2/logic2manager.py";

my $args = "--launch --exe_path $path --timeout $timeout --port $port";

my $cmd = "$pycli $py_script $args";
my $out1 = System_Command($cmd, "log", 1);

LogEntry("Result: ".$out1);

Now the thing is I cannot return ‘app’ a launch instance to my perl code and further use it inside my perl code.
May be I should launch Logic 2 process the following way and later use connect():

subprocess.Popen([‘C:\Program Files\Logic\Logic.exe’, ‘–automation’, ‘–automationPort’, ‘10429’], shell=True, creationflags=subprocess.CREATE_NEW_CONSOLE)

Yes, if you are calling a python script from Perl just to launch() the Logic application and calling another python script for more automation commands, then the Manager won’t persist between the separate calls and will close() itself at the end of the first python script.

So, you’ll need a way to spawn the Logic2 application separately – either via python or directly in Perl. Also, I think you might want a way to close/kill the Logic2 application when you’re all done, and/or track to see if it is already running (especially if you’re executing this script multiple times).

E.g., since it appears you’re on Windows, I think you could do something like: taskkill /IM Logic.exe /F at the start and/or when you’re all done and want to close the Logic2 application.

Finally, if you’re really ambitious – you may be able to use gRPC for Perl directly by adapting the saleae.proto file from the Saleae Logic2 Automation GitHub repository. I’m not sure how complicated it is to setup for another language. Below are some links, if you’re interested :slight_smile:

Note: While python is the only language Saleae directly supports, the protobuf definition is available to use any other gRPC supported language(s) directly. However, I think some of the Manager class (like launch() and connect() APIs) are python-specific helpers created by Saleae, so you’d need to implement equivalent features yourself in other languages separately (as they aren’t part of the lower-level gRPC protocol). The gRPC protocol will need the Logic2 application running (with automation server/port enabled) to work. You can view the saleae.proto file for more details on the API procedures implemented in the lower-level gRPC protocol.