Actiongroup called from a plugin

Posted on
Fri Oct 18, 2019 7:43 am
Umtauscher offline
User avatar
Posts: 566
Joined: Oct 03, 2014
Location: Cologne, Germany

Re: Actiongroup called from a plugin

I changed my script from
Applescript
Code: Select all
do shell script "/usr/bin/afplay /Users/wilhelm/Music/STARWAVE/c827.WAV"

to
Python
Code: Select all
import subprocess
returncode = subprocess.call("/usr/bin/afplay /Users/wilhelm/Music/STARWAVE/c827.WAV|wait",shell=True)

If that python script still terminates before the sound is played I would very much welcome your suggestion how to change it.

Cheers
Wilhelm

Posted on
Fri Oct 18, 2019 8:01 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Actiongroup called from a plugin

Try this:
Code: Select all
import os
import sys
import subprocess
import time

p = subprocess.Popen("/usr/bin/afplay /Users/wilhelm/Music/STARWAVE/c827.WAV",
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT)
try:
    # Filter stdout
    for line in iter(p.stdout.readline, ''):
        sys.stdout.flush()
        sys.stdout.flush()
except:
    sys.stdout.flush()

# Wait until process terminates (without using p.wait())
while p.poll() is None:
    # Process hasn't exited yet, let's wait some
    time.sleep(0.5)

# Get return code from process
return_code = p.returncode

# Exit with return code from process
sys.exit(return_code)


Not tested but should be close. :)

Posted on
Fri Oct 18, 2019 8:07 am
Umtauscher offline
User avatar
Posts: 566
Joined: Oct 03, 2014
Location: Cologne, Germany

Re: Actiongroup called from a plugin

Code: Select all
 
 Script Error                    embedded script: [Errno 2] No such file or directory
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 8, at top level
     File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
       errread, errwrite)
     File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1308, in _execute_child
       raise child_exception
OSError: [Errno 2] No such file or directory

The File or directory seems to be stderr=subprocess.STDOUT

Posted on
Fri Oct 18, 2019 8:21 am
Umtauscher offline
User avatar
Posts: 566
Joined: Oct 03, 2014
Location: Cologne, Germany

Re: Actiongroup called from a plugin

I just tested with server action run script. Do you suppose this is also started as a background process? Because it behaves exactly the same.
And afplay returns AFTER the sound is completely played, I checked that too.

Posted on
Fri Oct 18, 2019 8:27 am
Umtauscher offline
User avatar
Posts: 566
Joined: Oct 03, 2014
Location: Cologne, Germany

Re: Actiongroup called from a plugin

just found this on docs.python.org
Code: Select all
 subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

    Run command with arguments. Wait for command to complete.

I think this confirmes my initial theory that the script is already running in a paralell process - unless I am missing something here

Posted on
Fri Oct 18, 2019 8:36 am
Umtauscher offline
User avatar
Posts: 566
Joined: Oct 03, 2014
Location: Cologne, Germany

Re: Actiongroup called from a plugin

Sorry I copied the wrong line... :lol:
Code: Select all
 subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

    Run the command described by args. Wait for command to complete, then return the returncode attribute.

Posted on
Fri Oct 18, 2019 10:45 am
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: Actiongroup called from a plugin

Embedded python scripts run sequentially, but in their own process (independent of the Indigo Server).

That is actually a feature/advantage over how AppleScript executed, which occurred in the main Indigo Server thread. The result is the Indigo Server would hang while scripts ran (and even worse if the script caused a crash the Indigo Server would be taken down). This in particular was a problem for AppleScript that targeted other apps or remote Macs / services (they could hang for literally minutes).

The downside is a case like this where you want the synchronization of having it all run by the Indigo Server.

In this case I'm not sure there is a good or simple solution. The easiest solution would be if the plugin provided the ability to play the sound directly, then it could be sure it was synchronized to occur before/after the event correctly. Currently it is just handing off the execution of the Action Group to the Indigo Server which executes it as efficiently as it can.

I'll also add a feature request (I think we might already have it, but I'm not sure) to optionally have Indigo embedded python scripts block until they are finished executing.

Image

Posted on
Fri Oct 18, 2019 11:09 am
Umtauscher offline
User avatar
Posts: 566
Joined: Oct 03, 2014
Location: Cologne, Germany

Re: Actiongroup called from a plugin

Hi Matt,

thanks for the information.
Wouldn‘t it be simpler if indigo.actionGroup.Execute would have an option to wait for the completion?
To make it as compatible as possible, there could also be an option in the action group to execute it synchronous.

As my original Applescript does‘nt address the indigo server, could I just leave it as it is?

Cheers
Wilhelm

Posted on
Fri Oct 18, 2019 11:17 am
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: Actiongroup called from a plugin

Action executing in Indigo, especially via APIs, is a very asynchronous operation. Plugins and scripts run in their own processes, and actions in an Action Group can call out to other plugins (or execute scripts, etc.), not to mention multiple threads in the Indigo Server and potentially plugins. Trying to have everything wait/block is a good recipe for lots of deadlocks to occur, so in-general the blocking needs to occur at the most atomic level possible (in this case the execution of individual actions in an Action Group). So while I agree with you from a simplicity of API perspective, from an architectural level it isn't something we are going to do.

You can leave in actions that execute external AppleScript files, but Indigo has (and continues to) spin up a new process to execute those. So I'm not sure that will address the problem you are having.

Image

Who is online

Users browsing this forum: No registered users and 1 guest